IS a there a hook to update the social media image for posts

#83274
  • Resolved Saijo George
    Rank Math pro

    Hi

    Is there a function or hook I can call to update the Facebook and Twitter Image for a post after the post has been published?

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

    Thank you for contacting Rank Math today.

    You can use the following filter to change any social sharing property. Just customize and add it to your active theme functions.php:

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

    Looking forward to helping you. Thank you.

    ​​​​​​

    Hello,

    I have updated the sensitive data as requested. Can you please check further?

    Thank you.

    we did try that

    add_filter( "rank_math/opengraph/facebook/image", function( $img_path ) {
    return $img_path;
    });
    add_filter( "rank_math/opengraph/twitter/image", function( $img_path ) {
    return $img_path;
    });

    How to check the image is updated or not for the post? As you can see from the image it’s not reflected in the code that is added tot eh head

    Alberto
    Rank Math business

    Hello,

    In that case, we might need to take a closer look at the settings. Please edit the first post on this ticket and include your WP logins in the designated Sensitive Data section.
    Sensitive Data Section

    It is completely secure and only our support staff has access to that section. If you want, you can use the below plugin to generate a temporary login URL to your website and share that with us instead:

    https://wordpress.org/plugins/temporary-login-without-password/

    You can use the above plugin in conjunction with the WP Security Audit Log to monitor what changes our staff might make on your website (if any):

    https://wordpress.org/plugins/wp-security-audit-log/

    We really look forward to helping you.

    Hello,

    I have updated the sensitive data as requested. Can you please check further?

    Thank you.

    function.php line 1486

    Any updates on this ?

    Hello George,

    I couldn’t find a way to test if the it is working, which post type are you targetting with this? How are you testing if the variable $horizontal_img_path is containing the any URL?

    Also the correct syntax should be: add_filter( "rank_math/opengraph/facebook/og_image", function( $content ) note the og_image and not image

    Hope this helps.

    
    add_filter( "rank_math/opengraph/facebook/og_image", function( $horizontal_img_path ) {
                    return $horizontal_img_path;
                });
                add_filter( "rank_math/opengraph/twitter/og_image", function( $horizontal_img_path ) {
                    return $horizontal_img_path;
                });
    

    OR

    add_filter( "rank_math/opengraph/facebook/og:image", function( $horizontal_img_path ) {
                    return $horizontal_img_path;
                });
                add_filter( "rank_math/opengraph/twitter/twitter:image", function( $horizontal_img_path ) {
                    return $horizontal_img_path;
                });
    • This reply was modified 3 years, 6 months ago by Saijo George.

    Hello,

    The correct filters you need to use are rank_math/opengraph/facebook/image & rank_math/opengraph/twitter/image. Sorry for the confusion. I just tried this to confirm and the filters work as expected for me on a test site.

    Could you please tell us more about what you are trying to achieve with the custom code?

    The code snippets you included in your last post do not do anything, even when using the correct filters. The code in its current form simply takes the value and passes it on as-is, without any changes. If you want to change the og:image or twitter:image value, then you will have to adjust the code. Here’s an example of this:

    add_filter( 'rank_math/opengraph/facebook/image', function( $horizontal_img_path ) {
    	$post_meta = get_post_meta( get_the_id(), 'custom_img_horizontal', true );
    	if ( $post_meta ) {
    		return $post_meta;
    	}
    
        return $img_path;
    });

    This code will check if the post has a custom field called custom_img_horizontal and if it does, then it will use that for the og:image tag.

    Please note that these filters will only change the og:image/twitter:image tags. If the post doesn’t have the tag to begin with then it will not add one.

    Hope that helps. Looking forward to helping you.

    Alberto
    Rank Math business

    Hello,

    It is the first one, the one with og_ and not with og:.

    Looking forward to help you.

    Hello,

    I have updated the sensitive data as requested. Can you please check further?

    Thank you.

    Hey Balazs

    that worked thanks for your help.

    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.

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

You must be logged in to reply to this ticket.