Skip to content
This repository has been archived by the owner on Feb 6, 2019. It is now read-only.

How to add additional values to Site Reviews JSON LD schema

Paul Ryley edited this page Feb 20, 2018 · 2 revisions

Site Reviews only generates the basic required schema for what is being reviewed, along with the review and rating schema. Depending on what schema type you have set in the Settings (for example "LocalBusiness"), Google's Structured Data Testing Tool may show you warnings of missing recommended (but not required) values.

You have two options to add these additional missing values:

  1. Use a third-party JSON-LD schema WordPress plugin.

  2. Use the Site Reviews provided filter hooks to modify the schema array before it is printed to the page. You can find the documentation in the "Site Reviews -> Get Help -> Documentation -> Hooks" page.

Here is an example of how to use a filter hook in your theme's functions.php file to add the missing telephone, address, and priceRange values to a LocalBusiness schema:

/**
 * Adds the address, priceRange, and telephone to a LocalBusiness schema.
 */
add_filter( "site-reviews/schema/LocalBusiness", function( $schema ) {
    // Make sure to change these values to your own!
    $schema['address'] = [
        '@type' => 'PostalAddress',
        'streetAddress' => '1 Infinite Loop',
        'addressLocality' => 'Cupertino',
        'addressRegion' => 'CA',
        'postalCode' => '95014',
        'addressCountry' => [
            '@type' => 'Country',
            'name' => 'US',
        ],
    ];
    $schema['priceRange'] = '$$$-$$$$';
    $schema['telephone'] = '+18006927753';
    return $schema;
});

Here is a more advanced example that allows you to have different schema values for each post:

/**
 * Adds the address, priceRange, and telephone to a LocalBusiness schema from Custom Fields.
 * If no schema Custom Fields have been set, it will use the defaults.
 * Available Custom Field keys to use are:
 * - street
 * - city
 * - state
 * - zipcode
 * - countrycode
 * - pricerange
 * - telephone
 * @see https://codex.wordpress.org/Custom_Fields
 */
add_filter( "site-reviews/schema/LocalBusiness", function( $schema ) {
    // Make sure to change these default values to your own!
    $defaults = [
        'street' => '1 Infinite Loop',
        'city' => 'Cupertino',
        'state' => 'CA',
        'zipcode' => '95014',
        'countrycode' => 'US',
        'pricerange' => '$$$-$$$$',
        'telephone' => '+18006927753',
    ];
    $metaValues = array_map( function( $value ) {
        return is_array( $value ) ? reset( $value ) : $value;
    }, get_post_meta( get_the_ID() ));
    $meta = shortcode_atts( array_fill_keys( array_keys( $defaults ), '' ), $metaValues );
    if( !array_filter( $meta )) {
        $meta = $defaults;
    }
    $schema['address'] = [
        '@type' => 'PostalAddress',
        'streetAddress' => $meta['street'],
        'addressLocality' => $meta['city'],
        'addressRegion' => $meta['state'],
        'postalCode' => $meta['zipcode'],
        'addressCountry' => [
            '@type' => 'Country',
            'name' => $meta['countrycode'],
        ],
    ];
    $schema['priceRange'] = $meta['pricerange'];
    $schema['telephone'] = $meta['telephone'];
    return $schema;
});