Skip to main content

Rich Snippets

1. Code to remove json+ld data

/**
* Code to remove json+ld data
*
*/
add_action( 'rank_math/head', function() {
global $wp_filter;
if ( isset( $wp_filter["rank_math/json_ld"] ) ) {
unset( $wp_filter["rank_math/json_ld"] );
}
});

2. Filter to extend json+ld 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/json_ld', function( $data, $jsonld ) {
return [];
}, 99, 2);

3. Filter to remove BreadcrumbList Schema

/**
* Allow developer to disable the breadcrumb json-ld output.
*
* @param bool $unsigned Default: true
*/
add_filter( 'rank_math/json_ld/breadcrumbs_enabled', '__return_false' );

4. Filter to remove CollectionPage Schema on Taxonomy Pages

/**
* Allow developers to remove snippet data from Taxonomies.
*
* @param bool $unsigned Default: false
* @param string $unsigned Taxonomy Name
*/
add_filter( 'rank_math/snippet/remove_taxonomy_data', function( $value, $taxonomy ) {
return $value; // true or false
}, 10, 2);

5. Filter to disable the Review Display on the frontend

/**
* Filter: Allow disabling the review display.
*
* @param bool $return true to disable.
*/
add_filter( 'rank_math/snippet/review/hide_data', '__return_true' );

6. Filter to change the Review Display location

/**
* Filter to change the review display location
*
* @param string $location top, bottom, both or custom.
*/
add_filter( 'rank_math/snippet/review/location', function( $location, $taxonomy ) {
return $location;
});

7. Filter to change the Review Editor’s choice text

/**
* Filter to change review editor's choice text
*
* @param string $text Default Editor's choice.
*/
add_filter( 'rank_math/review/text', function( $text ) {
return $text;
});

8. Filter to change the Review Snippet HTML

/**
* Filter to change review snippet HTML
*
* @param string $html.
*/
add_filter( 'rank_math/snippet/html', function( $html ) {
return $html;
});

9. Filter to remove Schema Data from Posts

/**
* Filter to remove Schema Data from Posts.
* Replace $schema_type with schema name like article, review, etc.
* @param bool $value true/false Default false
* @param array $parts Post Data
* @param array $data Schema Data
*
* @return bool
*/
add_filter( "rank_math/snippet/rich_snippet_{$schema_type}", function( $value, $parts, $data ) {
return true;
}, 10, 3 );

10. Filter to change Post’s Front-end Schema data

/**
* 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;
});

11. Add Brand Name for Products

/**
* Filter to add Brand Name for Products.
*
* @param array $entity Snippet Data
* @return array
*/
add_filter( 'rank_math/snippet/rich_snippet_product_entity', function( $entity ) {
$entity['brand']['@type'] = 'Brand';
$entity['brand']['name'] = 'Rank Math';
return $entity;
});

12. Filter to remove worksFor property from the Post Author Schema

This filter is helpful when you have guest writers.

add_filter( 'rank_math/json_ld', function( $data, $jsonld ) {
if ( ! isset( $data['ProfilePage'] ) ) {
return $data;
}
global $post;
$author_id = is_singular() ? $post->post_author : get_the_author_meta( 'ID' );
if ( in_array( intval( $author_id ), [ 1, 2, 3 ], true ) ) {
unset($data['ProfilePage']['worksFor']);
}
return $data;
}, 99, 2);

13. Filter to Change the Shortcode Content

/**
* Filter to change the rank_math_rich_snippet shortcode content.
*
* @param string $shortcode_html
*/
add_filter( 'rank_math/snippet/html', function( $shortcode_html ) {
return $shortcode_html;
} );

14. Filter to remove worksFor from the Author Entity

If the post author is not working for your company or is a guest author, then use this filter to remove worksFor attribute from the Author entity.

/**
* Filter to remove worksFor from the Author Entity.
*/
add_filter( 'rank_math/json_ld', function( $data, $jsonld ) {
if ( ! isset( $data['ProfilePage'] ) ) {
return $data;
}
global $post;
$author_id = is_singular() ? $post->post_author : get_the_author_meta( 'ID' );
if ( in_array( $author_id, [ 1, 2, 3 ], true ) ) {
unset($data['ProfilePage']['worksFor']);
}
return $data;
}, 99, 2);

15. Filter to change the Attachment title of the video thumbnail

/**
* Filter the attachment title of the video thumbnail.
*
* @param string $attachment_title The attachment title of the video thumbnail.
* @param array $data The video data.
* @param object $post The post object.
*/
add_filter('rank_math/schema/video_thumbnail_attachment_title', function ($attachment_title, $data, $post) {
return $attachment_title;
}, 10, 3 );

16. Filter to change the Filename of the video thumbnail

/**
* Filter the filename of the video thumbnail.
*
* @param string $filename The filename of the video thumbnail.
* @param array $data The video data.
* @param object $post The post object.
*/
add_filter('rank_math/schema/video_thumbnail_filename', function ($filename, $data, $post) {
return $filename;
}, 10, 3 );

17. Filter to change the Pros and Cons labels

/**
* Filter: Allow changing the Pros & Cons labels.
*
* @param array $labels {
* @type string $pros Pros label.
* @type string $cons Cons label.
* }
*/
add_filter( 'rank_math/schema/review_notes_labels', function( $labels ) {
return $labels;
});

18. Filter to change the nested blocks to be parsed

/**
* Filter: 'rank_math/schema/nested_blocks' - Allows filtering for nested blocks.
*
* @param array $data Array of JSON-LD data.
* @param array $block The block.
*/
add_filter('rank_math/schema/nested_blocks', function( $nested ) {
return $nested;
});

19. Filter to change the content of Rank Math Blocks

Ensure that you change the $block variable in the below filter to specific blocks to howto, faq, etc

/**
* Filter to change the output of Rank Math Block.
*
* @param string $output Output of the Block.
* @param array $block_content Original block content before it is escaped.
* @param array $attributes Attributes of the Block.
*/
add_filter( 'rank_math/schema/block/$block/content', function ( $output, $block_content, $attributes ) {
return $output;
}, 10, 3 );

Here’s how to use the filter for TOC Block.

/**
* Filter to change the output of TOC Block.
*
* @param string $output Output of the TOC Block.
* @param string $block_content Unescaped Block content.
* @param array $attributes Attributes of the HowTo Schema Block.
*/
add_filter( 'rank_math/schema/block/toc/content', function ( $output, $block_content, $attributes ) {
return $output;
}, 10, 3 );

20. Filter to Disable SearchAction Schema

/**
* Filter to disable SearchAction Schema
*
*/
add_filter( 'rank_math/json_ld/disable_search', '__return_true' );