-
As I understand, this is not possible at the moment. However, it would be extremely user friendly as it would give us greater control over social media fields.
Thank you for this wonderful plugin, you are the best!
-
Hello,
Thank you for contacting Rank Math and bringing your concern to our attention.
It seems that you have marked this ticket as already resolved.
However, you can modify the social image by customizing this filter on your site to use the custom field value:
add_filter( 'rank_math/opengraph/facebook/og_image', function( $fbimage ) { global $post; $fbimage = get_field('social_image', $post->ID); /* Insert specific field name here */ return $fbimage; });
And here’s a guide to adding the code to the website:
https://rankmath.com/kb/wordpress-hooks-actions-filters/Hope that helps.
Thank you.
Reinelle, thank you for your feedback. As I am not a programmer, please explain a bit further the following:
1. Do I replace social_image with the custom field value?
2. Will this automatically add the same image to twitter as well?Again, thank you for everything.
Hello,
Thank you for your patience.
1. Yes, you may need to replace the
social_field
value with your actual custom field name.2. For Twitter, you may need to use this filter instead to update the
twitter:image
tag as well:add_filter( 'rank_math/opengraph/facebook/image', function( $fbimage ) { global $post; $fbimage = get_field('social_image', $post->ID); /* Insert specific field name here */ return $fbimage; }); add_filter( 'rank_math/opengraph/twitter/image', function( $fbimage ) { global $post; $fbimage = get_field('social_image', $post->ID); /* Insert specific field name here */ return $fbimage; });
Looking forward to helping you.
Reinelle, thank you so much for your help.
Where should I insert this code? In functions.php?Gina
Hello,
Yes, you can insert this code in the functions.php file or you can use the plugin Code Snippets for easier navigation as well as make sure that the code does not get lost if ever your theme needs updating:
https://wordpress.org/plugins/code-snippets/More information about that here:
https://rankmath.com/kb/wordpress-hooks-actions-filters/Please do not hesitate to let us know if you need our assistance with anything else.
Fantastic. One last thing please, I am using JetEngine from Crocoblock to add my meta fields. It does not allow me to add a field to posts and taxonomies at the same time. So, i will need to add 2 different image fields, one for posts and pages and the other for the taxonomies.
Posts and pages field is named social_image.
Taxonomy field is named taxonomy_social_image.Can i incorporate both fields as a custom image instead of RankMath’s original image within the same php code?
Thank you in advance for everything.
Hello,
Thank you for your patience.
You can set a conditional statement within the code to apply it for posts/pages, then the other one for the taxonomy pages.
Here’s the updated filter you can try and customize as well:
add_filter( 'rank_math/opengraph/facebook/image', function( $fbimage ) { global $post; if (is_singular() || is_page()) { $fbimage = get_post_meta($post->ID, 'social_image', true); return $fbimage; } elseif (is_category() || is_archive()) { $term_id = get_queried_object()->term_id; $fbimage = get_term_meta($term_id, 'taxonomy_social_image', true); return $fbimage; } }); add_filter( 'rank_math/opengraph/twitter/image', function( $twitterimage ) { global $post; if (is_singular() || is_page()) { $twitterimage = get_post_meta($post->ID, 'social_image', true); return $twitterimage; } elseif (is_category() || is_archive()) { $term_id = get_queried_object()->term_id; $twitterimage = get_term_meta($term_id, 'taxonomy_social_image', true); return $twitterimage; } });
Hope that helps.
Hello,
Since we did not hear back from you for 15 days, we are assuming that you found the solution. We are closing this support ticket.
If you still need assistance or any other help, please feel free to open a new support ticket, and we will be more than happy to assist.
Thank you.
The ticket ‘Can I add an image custom field as facebook and twitter image?’ is closed to new replies.