SEO Filters

#555605
  • Resolved Anonymous
    Rank Math free

    I’m having trouble figuring out how to use the SEO filters, specifically the one to change specific open graph social media tags:

    add_filter( "rank_math/opengraph/{$network}/$og_property", function( $content ) {
    return $content;
    });

    I was hoping you could give me a code snippet to demonstrate its use.

    I currently have the og:image working for a custom URL that I insert with the filter for adding an attachment URL, but I was trying to add a few extra properties to it (width, height and type).

    This is an example of what I was trying to do with this filter but was not having any luck:

    // add width for facebook
    add_filter( "rank_math/opengraph/facebook/image/width", function( $content ) {
    global $post;

    if ( ( is_single( $post->ID ) or is_page( $post->ID ) ) and ( !has_post_thumbnail( $post->ID ) ) ){
    $content = '1200';
    return $content;
    }

    });

    Thanks in advance for any tips you might be able to provide!

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

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

    You can try to use the filter like the following one to change your og properties for width, height, and type:

    add_filter( "rank_math/opengraph/facebook/og_image_width", function( $content ) {
    	$content = '1200';
    	return $content;
    });
    
    add_filter( "rank_math/opengraph/facebook/og_image_height", function( $content ) {
    	$content = '1200';
    	return $content;
    });
    
    add_filter( "rank_math/opengraph/facebook/og_type", function( $content ) {
    	$content = 'website';
    	return $content;
    });
    

    However, if you just want to change those og properties only for a specific post then you can try the following filter:

    add_filter( "rank_math/opengraph/facebook/og_type", function( $content ) {
        global $post;
        if($post->ID === YOUR_POST_ID_HERE){
            $content = 'website';
        }
        return $content;
    });
    

    Hope that helps and please don’t hesitate to let us know if you have any other questions.
     
    Thank you.

    Anonymous
    Rank Math free

    I’ve implemented a dynamic Open Graph (OG) image for posts that changes based on the post’s title to replace a static default image. I was able to get the OG properties listed above working as needed, so thanks for your help with that!

    One thing I am still trying to figure out is the post’s front end schema data. Is it possible to change the image URL in the schema data for the article type to point to the new dynamic image? When I used the below filter for changing the schema data, I didn’t see the image property returned in the listing of data associated with the $entity variable.

    add_filter( "rank_math/snippet/rich_snippet_article_entity", function( $entity ) {
     return $entity;
    });

    Doing var_dump($entity); did not show an option for the image URL in the resulting array. Is it possible to change this property?

    Hello,

    Please try adding the following filter to customize the URL in the schema:

    add_filter( 'rank_math/json_ld', function( $data, $jsonld ) {
    
        //add if/switch conditions for the below line if needed.
        $data['primaryImage']['url'] = 'YOUR IMAGE URL HERE';
    
        return $data;
    }, 99, 2);

    Let us know how it goes. Looking forward to helping you.

    Anonymous
    Rank Math free

    That worked; thanks!

    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.

    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 5 replies - 1 through 5 (of 5 total)

The ticket ‘SEO Filters’ is closed to new replies.