• Resolved Dylan Houlihan

    (@dylanhoulihan)


    Hey,

    I’m trying to create custom schema data for EmployerAggregateReview type, but it’s currently throwing some warnings.

    Here’s my code:

    add_filter('site-reviews/schema/EmployerAggregateRating', function ($schema) {
        $post_id = get_the_ID();
    
        $ratingInfo = apply_filters('glsr_get_ratings', null, [
            'assigned_posts' => $post_id,
        ]);
    
        // modify the $schema array here.
        $schema['ratingValue'] = $ratingInfo->average;
        $schema['ratingCount'] = $ratingInfo->reviews;
        $schema['bestRating'] = '5';
        $schema['worstRating'] = '1';
        $schema['itemReviewed'] = [
            '@type' => 'Organization',
            'name' => get_post_meta($post_id, 'organization_name', true),
            'sameAs' => get_post_meta($post_id, 'organization_url', true),
        ];
    
        return $schema;
    });

    This does what I want, but there’s some default schema leftover that’s causing a warning. Also, there’s some unnecessary data being displayed.

    Here’s what I’d like to remove:

    – AggregateRating (isn’t recognized by the EmployerAggregateRating type)
    – Description (isn’t necessary)
    – URL (isn’t necessary)
    – Image (isn’t necessary)

    Is this possible?

Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Author Gemini Labs

    (@geminilabs)

    Plugin Author Gemini Labs

    (@geminilabs)

    You can also add the following line: glsr_log($schema); before the return statement and then check the Console log.

    Thread Starter Dylan Houlihan

    (@dylanhoulihan)

    Thanks, I think my question was unclear. Sorry about that.

    Basically, my schema output currently looks like this

    I want it to look something like this.

    I guess instead of adding additional values to the default schema that the plugin provides, I want the code above to be the only values that are output.

    Plugin Author Gemini Labs

    (@geminilabs)

    Add the following line: glsr_log($schema); before the return statement and then reload the page and check the Console log on the Site Reviews Tools page. This will show you the contents of the schema array and help you know what keys to unset, set, etc.

    Thread Starter Dylan Houlihan

    (@dylanhoulihan)

    Ohhh thanks for clarifying. I’m still learning php so I didn’t even know about the unset function. Easy fix once I got that worked out.

    Although my code might not be the cleanest, this is it for anyone reading this in the future:

    add_filter('site-reviews/schema/EmployerAggregateRating', function ($schema) {
        $post_id = get_the_ID();
    
        $ratingInfo = apply_filters('glsr_get_ratings', null, [
            'assigned_posts' => $post_id,
        ]);
    
        // modify the $schema array here.
        $schema['ratingValue'] = $ratingInfo->average;
        $schema['bestRating'] = '5';
        $schema['worstRating'] = '1';
        $schema['ratingCount'] = $ratingInfo->reviews;
        $schema['itemReviewed'] = [
            '@type' => 'Organization',
            'name' => get_post_meta($post_id, 'organization_name', true),
            'sameAs' => get_post_meta($post_id, 'organization_url', true),
        ];
        unset($schema[aggregateRating]);
        unset($schema[description]);
        unset($schema[name]);
        unset($schema[url]);
        return $schema;
    });

    Schema returns exactly how I want to now. Thanks again. Really cool how customizable this plugin is. Excited to see what else I can do.

    Plugin Author Gemini Labs

    (@geminilabs)

    add_filter('site-reviews/schema/EmployerAggregateRating', function ($schema) {
        $post_id = get_the_ID();
        $schema['bestRating'] = $schema['aggregateRating']['bestRating'];
        $schema['worstRating'] = $schema['aggregateRating']['worstRating'];
        $schema['ratingCount'] = $schema['aggregateRating']['ratingCount'];
        $schema['ratingValue'] = $schema['aggregateRating']['ratingValue'];
        $schema['itemReviewed'] = [
            '@type' => 'Organization',
            'name' => get_post_meta($post_id, 'organization_name', true),
            'sameAs' => get_post_meta($post_id, 'organization_url', true),
        ];
        unset($schema['aggregateRating']);
        unset($schema['description']);
        unset($schema['name']);
        unset($schema['url']);
        return $schema;
    });

    Make sure to validate your schema (https://search.google.com/test/rich-results) after any custom modifications.

    Thread Starter Dylan Houlihan

    (@dylanhoulihan)

    Thanks, that fixed some php errors I was getting.

    Only change I had to make in your code was instead of $schema[‘ratingCount’] = $schema[‘aggregateRating’][‘ratingCount’]; it should be $schema[‘ratingCount’] = $schema[‘aggregateRating’][‘reviewCount‘];

    Plugin Author Gemini Labs

    (@geminilabs)

    Are you sure it’s not this instead?

    $schema['reviewCount'] = $schema['aggregateRating']['reviewCount'];
    

    See: https://schema.org/EmployerAggregateRating

    Thread Starter Dylan Houlihan

    (@dylanhoulihan)

    I guess it can be either: Google’s guidelines

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Remove Default Schema Data | Start Fresh’ is closed to new replies.