Skip to main content

OpenGraph

1. Filter to change OpenGraph Type ( og:type ).

Use this filter to change the OpenGraph type for the specific page on the front-end.

/**
* Allows theme developers to change the OpenGraph type of the page.
*
* @param string $type The OpenGraph type string.
*/
add_filter( 'rank_math/opengraph/type', function( $type ) {
return $type;
});

2. Filter to change OpenGraph URL ( og:url ).

/**
* Allows the output of the canonical URL as OpenGraph URL, consolidating likes and shares.
*
* @link https://developers.facebook.com/docs/reference/opengraph/object-type/article/
* @param $url Canonical URL
*/
add_filter( 'rank_math/opengraph/url', function( $url ) {
return $url;
});

3. Filter to remove Twitter tags from page output.

/**
* Allows changing the Twitter Card type as output in the Twitter card.
*
* @param string $type
*/
add_filter( 'rank_math/opengraph/twitter_card', '__return_false' );

4. Filter to change OpenGraph Image.

/**
* Allows developers to change the OpenGraph image within theme.
*
* The dynamic part of the hook name. $network, is the network slug. Can be facebook or twitter.
*
* @param string $attachment_url The image we are about to add.
*/
add_filter( "rank_math/opengraph/{$network}/image", function( $attachment_url ) {
return $attachment_url;
});

5. Filter to allow developers to add additional images.

/**
* Allows developers to add images to the OpenGraph tags within the theme.
*
* The dynamic part of the hook name. $network, is the network slug. Can be facebook or twitter.
*
* @param Image The current object.
*/
add_filter( 'rank_math/opengraph/{$network}/add_additional_images', function( $image ) {
return $image;
});

6. Filter to prevent setting default image in OpenGraph.

/**
* Passing a truthy value to the filter will effectively short-circuit the
* set default image process.
*
* @param bool $return Short-circuit return value. Either false or true.
*/
add_filter( 'rank_math/opengraph/pre_set_default_image', '__return_true' );

7. Filter to prevent setting image from content when image is not added in OpenGraph.

/**
* Passing a truthy value to the filter will effectively short-circuit the
* set content image process.
*
* @param bool $return Short-circuit return value. Either false or true.
* @param int $post_id Post ID for the current post.
*/
add_filter( 'rank_math/opengraph/pre_set_content_image', function( $return, $post_id ) {
return $return; // Set to true if you don't want to set image from Content.
});

8. Filter to determine image size to show in og:image, twitter:image.

/**
* Determines which image sizes we'll loop through to get an appropriate image.
*
* @param unsigned array - The array of image sizes to loop through. Default array( 'full', 'large', 'medium_large' )
*/
add_filter( 'rank_math/opengraph/image_sizes', function( $sizes ) {
return $sizes;
});

9. Filter to change specific social meta tags.

This filter lets you change specific social meta tags dynamically on the front-end.

/**
* Allow developers to change the content of specific social meta tags.
*
* The dynamic part of the hook name. $network, is the network slug
* and $og_property, is the property which we are outputting.
*
* @param string $content The content of the property.
*/
add_filter( "rank_math/opengraph/{$network}/$og_property", function( $content ) {
return $content;
});

10. Filter to change twitter card type.

/**
* Allow changing the Twitter Card type as output in the Twitter card.
*
* @param string $type
*/
add_filter( 'rank_math/opengraph/twitter/card_type', function( $type ) {
return $type;
});

11. A Hook to remove OpenGraph tags.

/**
* Hook to remove og:tags
*/
add_action( 'rank_math/head', function() {
remove_all_actions( 'rank_math/opengraph/facebook' );
remove_all_actions( 'rank_math/opengraph/twitter' );
});

12. Prevent the Output of the Price

/**
* Allow developers to prevent the output of the price in the OpenGraph tags.
*
* @param bool unsigned Defaults to true.
*/
add_filter( 'rank_math/woocommerce/og_price', '__return_false' );

Use this filter to remove the product:price:amount & product:price:currency meta tags.

13. Change Text to Calculate the Time to Read

/**
* Filter: Change the text to calculate the time to read.
*
* @param string $content The content of the post.
*/
add_filter('rank_math/frontend/time_to_read_content', function ($post_content) {
return wp_strip_all_tags($post_content);
});

14. Change the Words Per Minute to Calculate the Time to Read

/**
* Filter: Change the words per minute to calculate the time to read.
*/
add_filter( 'rank_math/frontend/time_to_read_wpm', function() {
return 200; //Words Per Minute
} );

15. Filter to Allow CDN Path in the Overlay Image

/**
* Filter: 'rank_math/social/create_overlay_image' - Change the create_overlay_image arguments.
*/
add_filter('rank_math/social/create_overlay_image', function ($args) {
return $args;
}, 10, 1);