SEO PHP Intermediate

Add Organization and WebSite Schema to the Homepage

Tell search engines your organisation's name, logo and social profiles, and which name to show for your site in results.

Tested on WordPress 7.1 PHP 8.3 No plugin needed Updated
functions.php 30 lines
add_action( 'wp_head', function (): void {
	if ( ! is_front_page() || defined( 'WPSEO_VERSION' ) || defined( 'RANK_MATH_VERSION' ) || defined( 'SLIM_SEO_VER' ) || defined( 'SEOPRESS_VERSION' ) || defined( 'AIOSEO_VERSION' ) ) {
		return;
	}

	$name = wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES );
	$logo = has_custom_logo() ? wp_get_attachment_image_url( (int) get_theme_mod( 'custom_logo' ), 'full' ) : get_site_icon_url( 512 );

	$organization = array_filter( [
		'@type'  => 'Organization',
		'@id'    => home_url( '/#organization' ),
		'name'   => $name,
		'url'    => home_url( '/' ),
		'logo'   => $logo ?: null,
		'sameAs' => [
			'https://www.linkedin.com/company/example',
			'https://x.com/example',
		],
	] );

	$website = [
		'@type'     => 'WebSite',
		'@id'       => home_url( '/#website' ),
		'name'      => $name,
		'url'       => home_url( '/' ),
		'publisher' => [ '@id' => home_url( '/#organization' ) ],
	];

	echo '<script type="application/ld+json">' . wp_json_encode( [ '@context' => 'https://schema.org', '@graph' => [ $organization, $website ] ], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_HEX_TAG ) . "</script>\n";
} );

Where to add this

Recommended A code snippets plugin Survives theme changes and is easy to switch off.
Child theme At the end of your child theme's functions.php file.
Must-use plugin As a .php file in wp-content/mu-plugins, starting with <?php.

Always add PHP to a staging site first, or have FTP or file manager access ready, so you can undo it if something goes wrong.

Don't use with Yoast SEO, Rank Math, Slim SEO, SEOPress, All in One SEO. Check the notes below before adding it.

Structured data on your homepage tells search engines who runs the site: the organisation’s name, logo and official social profiles. The WebSite part also tells Google which name to show for your site above search results.

How it works

On the front page only, the snippet prints a JSON-LD @graph with two linked items. The Organization uses your site title, your Customizer logo (or the site icon if there’s no logo) and the profile URLs in sameAs. The WebSite uses the same name and points to the Organization as its publisher.

Empty values are removed, so you never output an empty logo field.

Things to watch

  • Replace the sameAs links with your real profiles, or remove the lines.
  • Google no longer shows the sitelinks search box, so there’s no need to add a SearchAction.
  • SEO plugins output their own version of this. The snippet does nothing if one is active.

How to test it

Paste your homepage URL into the Rich Results Test or validator.schema.org. You should see an Organization and a WebSite with no errors.

This website uses cookies to enhance your browsing experience and ensure the site functions properly. By continuing to use this site, you acknowledge and accept our use of cookies.

Accept All Accept Required Only