Skip to contentSkip to main navigation Skip to footer

How to Use a Custom Hook in WordPress for areaServed Schema

Schema.org provides a set of standardized tags that can be added to your website’s HTML to help search engines and other web services understand the content and context of your website. The areaServed schema is one of the most important schemas because it helps search engines understand where your business or organization is located and which geographic areas you serve.

In this tutorial, we will show you how to use a Squirrly SEO custom hook in WordPress to add the areaServed schema to your website’s pages.

Step 1: Create a custom hook in functions.php

The first step is to create a custom hook that will add the areaServed schema to your website’s home pages. To do this, add the following code to your functions.php file:

add_filter('sq_structured_data_type_for_page', function($type){ 
  $type[] = 'PoliceStation'; 
  return $type; 
});

This code creates a filter hook that adds the ‘PoliceStation’ schema type to your website’s pages.

Step 2: Add the areaServed schema to your page. To do this, add the following code to your functions.php file:

add_filter('sq_json_ld_data', function($data){ 
  global $post;
  if(isset($post->ID) && $post->ID > 0) {
	$current = SQ_Classes_ObjController::getClass( 'SQ_Models_Frontend' )->setPost( $post )->getPost();
	if ( $current->post_type === 'home' ) {

		$data[] = [
			"@context"        => "https://schema.org",
			"@type"           => "PoliceStation",
			"name"            => "Youngstown Central Police Station",
			"description"     => "Central police station",
			"openingHours"    => "Mo-Su",
			"telephone"       => "+13303301888",
			"email"           => "ohio.police.example@example.com",
			"image"           => "https://homepages.cae.wisc.edu/~ece533/images/lena.png",
			"address"         => [
				"@type"           => "PostalAddress",
				"addressLocality" => "Youngstown",
				"addressRegion"   => "OH",
				"postalCode"      => "44507",
				"streetAddress"   => "270  Derek Drive"
			],
			"areaServed"      => [
				"@type"       => "GeoCircle",
				"geoMidpoint" => [
					"@type"     => "GeoCoordinates",
					"latitude"  => 41.108237,
					"longitude" => - 80.642982
				],
				"geoRadius"   => 1000
			],
			"aggregateRating" => [
				"@type"       => "AggregateRating",
				"bestRating"  => 100,
				"worstRating" => 0,
				"ratingValue" => 88,
				"reviewCount" => 122
			]
		];
	}
  }

  return $data;
},11);

This code adds the areaServed schema to the ‘home’ page type.

You can modify this code to add the schema to other page types or to add additional schema types.

The following hook will add the areaServed schema on all pages.

add_filter('sq_json_ld_data', function($data){
	$data[] = [
		"@context"        => "https://schema.org",
		"@type"           => "PoliceStation",
		"name"            => "Youngstown Central Police Station",
		"description"     => "Central police station",
		"openingHours"    => "Mo-Su",
		"telephone"       => "+13303301888",
		"email"           => "ohio.police.example@example.com",
		"image"           => "https://homepages.cae.wisc.edu/~ece533/images/lena.png",
		"address"         => [
			"@type"           => "PostalAddress",
			"addressLocality" => "Youngstown",
			"addressRegion"   => "OH",
			"postalCode"      => "44507",
			"streetAddress"   => "270  Derek Drive"
		],
		"areaServed"      => [
			"@type"       => "GeoCircle",
			"geoMidpoint" => [
				"@type"     => "GeoCoordinates",
				"latitude"  => 41.108237,
				"longitude" => - 80.642982
			],
			"geoRadius"   => 1000
		],
		"aggregateRating" => [
			"@type"       => "AggregateRating",
			"bestRating"  => 100,
			"worstRating" => 0,
			"ratingValue" => 88,
			"reviewCount" => 122
		]
	];

	return $data;
},11);

Step 3: Test your schema

To test your schema, you can use the Google Structured Data Testing Tool. Enter the URL of the page you want to test, and the tool will show you the structured data that it detects on your page.

Was This Article Helpful?

0 Comments

There are no comments yet

Leave a comment