Follow-up: “thumbnailUrl” to Schema Markup

#85930
  • Resolved Artem Rev
    Rank Math free

    Hello,

    My other ticket was closed prematurely because the code that was given to me did not work.

    In the original ticket located here: https://support.rankmath.com/ticket/adding-thumbnailurl-to-schema-markup/

    I was trying to get the ‘thumbnailUrl’ to display the featured image of the post.

    The code which was suggested was:

    /**
    * 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 ) {
    	if(is_single()){
               // If it is a post
               // Get its feature post URL:
               // grab the url for the full size featured image
               $featured_img_url = get_the_post_thumbnail_url(get_the_ID(),'full'); 
    
               // Now add that URL to the ThumbnailURL schema
    	   $data['thumbnailURL'] = [
    			$featured_img_url
    		];
    	    return $data;
    	}
    	return [];
    }, 10, 2);

    But this code gave an Unspecified Type error as shown in the screenshot link below. I think it was because this schema markup was not placed INSIDE the article schema but outside of it.

    https://snipboard.io/gu7eIl.jpg

    Is it possible to change this code so that it places the ‘thumbnailUrl’ inside the article schema?

    Please advise.

    Regards.

    • This topic was modified 4 years, 3 months ago by Artem Rev.
    • This topic was modified 4 years, 3 months ago by Artem Rev.
    • This topic was modified 4 years, 3 months ago by Artem Rev.
Viewing 7 replies - 1 through 7 (of 7 total)
  • Hello,

    Thank you for contacting Rank Math today.

    To modify existing schema type, in your case article schema, you can would need to use the filter below:

    
    /**
     * Filter to change the schema data.
     * Replace $schema_type with schema name like article, review, etc.
     * @param array $entity Snippet Data
     * @return array
     */
    add_filter( "rank_math/snippet/rich_snippet_{$schema}_entity", function( $entity ) {
     return $entity;
    });

    Looking forward to helping you. Thank you.

    ​​​​​​

    Hello,

    Thank you for the reply.

    I’ve added the following code as per your suggestion with the modifications I THINK might work. Again, I am no coder, I have a very basic idea of how any of this works.

    This is my code:

    add_filter( "rank_math/snippet/rich_snippet_{$schema}_entity", function( $entity ) {
     $featured_img_url = get_the_post_thumbnail_url(get_the_ID(),'full'); 
    	return $entity['thumbnailURL'] = '$featured_img_url';
    	return $entity;
    });

    However, once emptying all caches and checking an article link in the structured data testing tool, the “thumbnailURL” does not appear in the Schema Markup.

    Am I doing something wrong here with the code?

    Please advise.

    Alberto
    Rank Math business

    Hello,

    You need to replace {$schema} in this line:
    add_filter( "rank_math/snippet/rich_snippet_{$schema}_entity", function( $entity ) {

    With the schema you want to modify. For example, the Article schema would be like this:
    add_filter( "rank_math/snippet/rich_snippet_article_entity", function( $entity ) {

    Looking forward to help you.

    Hello Alberto,

    I’ve made the changes as you suggested. Here is the code I used:

    add_filter( "rank_math/snippet/rich_snippet_article_entity", function( $entity ) {
     $featured_img_url = get_the_post_thumbnail_url(get_the_ID(),'full'); 
    	return $entity['thumbnailURL'] = '$featured_img_url';
    });

    When I ran the structured data testing tool, ALL of the rank math article schema was gone and was replaced just by “$featured_img_url”.

    I do NOT want to replace everything, I just want to add to the existing schema a single line of ‘thumbnailURL’ = featured image URL.

    How can I ADD to the existing data and not replace everything?

    Hello,

    Please use the following and let us know:

    
    add_filter( "rank_math/snippet/rich_snippet_article_entity", function( $entity ) {
     $featured_img_url = get_the_post_thumbnail_url(get_the_ID(),'full'); 
     $entity['thumbnailURL'] = '$featured_img_url';
     return $entity;
    });
    

    Looking forward to helping you. Thank you.

    ​​​​​​

    Hello Michael,

    The code you gave was placing the “thumbnailURL” inside the article schema, but the URL was just “$featured_img_url” and not the actual URL. This is because there was extra (”) quotation marks around the parameter. After I removed them it worked.

    For anyone else who might find this in the future. Here is the final code that worked.

    /**
    * Adds ThumbnailURL for Each Post into RankMath Schema Data
    * 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/snippet/rich_snippet_article_entity", function( $entity ) {
     $featured_img_url = get_the_post_thumbnail_url(get_the_ID(),'full'); 
     $entity['thumbnailURL'] = $featured_img_url;
     return $entity;
    });
    • This reply was modified 4 years, 3 months ago by Artem Rev.
    Alberto
    Rank Math business

    Hello,

    We are super happy that this resolved your issue. If you have any other questions in the future, know that we are here to help you.

    Also, thank you for sharing your code, I am sure it will help a lot of people!

    If you don’t mind me asking, could you please leave us a review (if you haven’t already) on https://wordpress.org/support/plugin/seo-by-rank-math/reviews/#new-post about your overall experience with Rank Math? We appreciate your time and patience.

    If you do have another question in the future, please feel free to create a new forum topic, and it will be our pleasure to assist you again.

    Thank you.

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

You must be logged in to reply to this ticket.