-
Hello. I use Rank Math as my main SEO. Currently i’m building a site for a client, that only uses ACF to control my page builder design on the front end.
I’d like to know if I can use ACF to add a custom Facebook open graph image on each individual post…instead of using Rank Math’s social media snippet tab.
I have a sample of the back end interface the client will use
-
Hello,
Thank you for contacting the support, and sorry for any inconvenience that might have been caused due to that.
The OG tags are created automatically so you would need to manually assign the ACF fields using our filters.
Here’s an example:
add_filter( 'rank_math/opengraph/facebook/og_title', function( $title ) { global $post; $title = get_field('my_field_name', $post->ID); /* Insert specific field here */ return $title; });Here’s our detailed guide: https://rankmath.com/kb/filters-hooks-api-developer/#opengraph
Hope that helps and please do not hesitate to let us know if you need our assistance with anything else.
Thank you for your quick response. I see though that this is for the title. I’m looking to change customize the OG image through an ACF image field. It looks like that’s possible? When I input your code, the title showed the URL for the image file.
So if I’m to follow your link for samples, it would be the following for a custom OG image?
add_filter( “rank_math/opengraph/{$network}/image”, function( $attachment_url ) {
return $attachment_url;
$attachment_url = get_field(‘social_og’, $post->ID); /* Insert specific field here */
return $attachment_url;
});Hello,
The filter I shared is just an example to modify the title, you can simply change the parameters to your liking as stated on the guide link I provided.
As for the filter you shared, I would suggest to specify the
$networkvariable to either “facebook” or “twitter”.Also, you returned the
$attachment_urlvariable twice so please remove the first one.Let me know if that will work from your end.
Looking forward to helping you.
Thanks. This code worked. This doesn’t replace the default OG image…but only affects it for that 1 post that select the custom image?
add_filter( ‘rank_math/opengraph/facebook/og_image’, function( $attachment_url ) {
global $post;
$attachment_url = get_field(‘social_og’, $post->ID); /* Insert specific field here */
return $attachment_url;
});Hello,
The code will only work on a single post or page where the specific ACF is present. Kindly check if it’s working on another post with the same ACF field.
If you do wish to make use of the global OpenGraph image, you can instead do it in Rank Math’s Global Meta settings. Please head over to your WordPress Dashboard > Rank Math > Titles & Meta > Global Meta and assign an image to “OpenGraph Thumbnail”:
Hope that helps and please do not hesitate to let us know if you need our assistance with anything else.
Yes…it did work on another post. Took awhile for FB’s OG to pick it up.
Thanks for your help
Hello,
I am glad that helped you. Could you please confirm if this resolves your issue?
Looking forward to helping you.
Yes. This seems to have worked.
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.
Hi. I’m sorry…I do have another question. This filter targets Facebook. Is there a way to target Twitter at the same time?
add_filter( ‘rank_math/opengraph/facebook/og_title’, function( $title )
Hello,
You can target twitter by replacing the facebook and the og_title in the filter:
add_filter( ‘rank_math/opengraph/twitter/twitter_title’, function...I hope that helps.
Thank you.
Thanks for your help. I get a PHP error when loading my page. I’ve put both Facebook and Twitter in the same filter. Do they have to be separate filters?
add_filter( ‘rank_math/opengraph/facebook/og_title’,’rank_math/opengraph/twitter/twitter_title’, function( $title ) {
global $post;
$title = get_field(‘social_title’, $post->ID); /* Insert specific field here */
return $title;
});Hello,
Yes, they would need to be separated as their functions are different from each other. So, you will need to add 2 filters for them to work on your site.
Looking forward to helping you.
Thank you.
I’m slowly piecing ACF and Rank Math together..and I have ACF set to change custom items for social media posts.
Is it possible to connect ACF to populate the “focus keyword” with text from an ACF text field?
Hello,
Yes, it is possible, but with a help of a different code. Please refer to our code here:
function update_focus_keywords() { $posts = get_posts(array( 'posts_per_page' => -1, 'post_type' => 'post' //replace post with the name of your post type )); foreach ($posts as $p) { update_post_meta($p->ID, 'rank_math_focus_keyword', strtolower(get_field('YOUR_ACF_NAME', $p->ID))); } } add_action('init', 'update_focus_keywords');This should loop all your posts and will automatically insert the focus keyword from your ACF. Please edit the code as per requirement.
Here’s a full guide when inserting keywords automatically: https://rankmath.com/kb/how-to-automate-the-insertion-of-focus-keyword/
Hope that helps and please do not hesitate to let us know if you need our assistance with anything else.
The ticket ‘Custom Open Graph with ACF’ is closed to new replies.