In this knowledgebase article, we’re going to walk you through how to use ACF fields in Rank Math’s Schema Generator.
What is Advanced Custom Fields Plugin?
Advanced Custom Fields, aka ACF, is a WordPress plugin that allows you to add extra content fields known as Custom Fields and helps to tailor what fields to show. Advanced Custom Fields lets you build websites faster and quickly educate your customers.
Using Advanced Custom Fields with Rank Math, you can enrich your pages and blog posts with more data and information with an easy-to-use solution. In addition, it’s effortless to integrate Rank Math with Advanced Custom Fields.
How to Use ACF Fields in Schema Generator?
The custom field variable (%customfield%
) will not work on the ACF fields. This is due to how ACF stores values in the post meta table. It stores values in a serialized format for groups, and for other fields, it stores key-value pairs.
You can define a new variable using this filter to get the value from the ACF field and use it in the Schema Generator. Here’s how we register the WooCommerce variable in the Rank Math plugin.
We recommend using the rank_math/json_ld filter to generate FAQ schema on the front end instead of adding it from the Schema generator.
You can do this by adding the following code to your theme’s rank-math.php file:
add_filter( 'rank_math/json_ld', function( $data, $jsonld ) {
if( ! have_rows('acf-repeater-faq') ) {
return $data;
}
$data['faqs'] = [
'@type' => 'FAQPage',
];
while( have_rows('acf-repeater-faq') ) {
the_row();
$data['faqs']['mainEntity'][] = [
'@type' => 'Question',
'name' => esc_attr( get_sub_field('faq_question') ),
'acceptedAnswer' => [
'@type' => 'Answer',
'text' => esc_attr( get_sub_field('faq_answer') ),
],
];
}
return $data;
}, 10, 2 );
The first step is to navigate to WordPress Dashboard → Appearance → Theme File Editor (for a Classic Theme).
For a Block Theme like Divi, navigate to WordPress Dashboard → Tools → Theme File Editor.
Now select the theme’s rank-math.php file.
Then add the above code and click the Update File button.
Also, please ensure that you change the acf-repeater-faq, faq_question, faq_answer
fields as per the settings in the ACF:
You can also read our knowledgebase article How to Use ACF Repeater Fields to Automate FAQ Schema to help you add and manage custom fields easily. If you still have any questions about how to use ACF fields in the Schema Generator – you’re always more than welcome to contact our dedicated support team.