How can I add reviewedBy programatically please?

#538033
  • Resolved Tristan
    Rank Math free

    Hello, I am looking to add the reviewedBy schema option (here: https://schema.org/reviewedBy ) (to WebPage) programatically.

    I am looking to do this programatically because I want to only add it for a single author, so ideally I will add this as an “add_filter” option (where I check the author, and if the ID matches, then I add reviewedBy in that one case).

    Could you please guide me on the best way forward? Thank you!

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hello,

    Thank you for contacting Rank Math and bringing your concern to our attention.

    Rank Math makes it possible for you to take a control of your SEO and you can easily extend the Rank Math JSON-LD output by making use of the following filter:

    /**
     * Collect data to output in JSON-LD.
     *
     * @param array  $unsigned An array of data to output in json-ld.
     * @param JsonLD $unsigned JsonLD instance.
     */
    add_filter( 'rank_math/json_ld', function( $data, $jsonld ) {
    	return [];
    }, 10, 2);

    You can add the condition for the author ID and add your custom reviewedBy schema.

    The filter should be added to your active theme’s functions.php file. Here’s another way to apply the filter using the rankmath.php file:
    https://rankmath.com/kb/filters-hooks-api-developer/#adding-filters-and-hooks-in-rank-math-php

    Hope that helps.

    Thank you.

    Thanks Reinelle, that set me on my way.

    For anyone else Googling this, I ended up using the following sort of code based on Reinelle’s starting point:

    		add_filter( 'rank_math/json_ld', function( $data, $jsonld ) {
    			if(!is_single()) return $data;
    			
    			if (get_the_author_meta('user_login') === 'author.login.id') {
    			   $author = array();
    			   $author["@type"] = 'Person';
    			   $author["@id"] = 'https://www.domain.com/author/reviewerid/';
    			   $author["url"] = 'https://www.domain.com/author/reviewerid/';
    			   $author["name"] = 'Reviewer Name';
    			
    			   $data['WebPage']['reviewedBy'] = $author;
    			   $data['WebPage']['lastReviewed'] = $data['WebPage']['dateModified'];
    			}
    			
    			return $data;
    		}, 99, 2);

    Hello,

    Glad that helped and thank you for sharing the code.

    Please feel free to reach out to us again in case you need any other assistance.

    We are here to help.

    Thank you.

    Hello,

    Since we did not hear back from you for 15 days, we are assuming that you found the solution. We are closing this support ticket.

    If you still need assistance or any other help, please feel free to open a new support ticket, and we will be more than happy to assist.

    Thank you.

Viewing 3 replies - 1 through 3 (of 3 total)

The ticket ‘How can I add reviewedBy programatically please?’ is closed to new replies.