Integrate ACF into Schema

#907835
  • Resolved Swan Creative
    Rank Math free

    Hiya, we’ve created ACF text fields to address schema but want this to autopopulate the schema fields in Rank Math, and then use the shortcode supplied in RankMath to show data on the site.

    The following code, taken from your site doesn’t seem to work.

    I’ve created a rankmath.php (please note the instructions on the site state a file called “rank-math.php” however this throws a critical error) file in my child which has the following code in:

    https://pastebin.com/DhWWdkc6

    Thanks,

    Matt

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

    We apologize for the issue you are facing with integrating ACF into the Schema in Rank Math. It seems like you are trying to use the provided code to autopopulate schema fields but are encountering a critical error.

    To ensure that ACF fields work correctly with Rank Math’s Schema Generator, please follow these steps:

    1. File Location: Make sure that you are editing the correct file. If you are using a child theme, the file should be named rank-math.php and placed in your child theme’s directory. The naming is crucial, so ensure there are no typos.

    2. Code Snippet: The code snippet you provided from Pastebin should be correctly formatted and placed within the rank-math.php file. Here’s a basic example of how the code should look:

      add_filter( 'rank_math/json_ld', function( $data, $jsonld ) {
         if( ! have_rows('faq-acf-repeater') ) {
             return $data;
         }
         $data['faqs'] = [
             '@type' => 'FAQPage',
         ];
         while( have_rows('faq-acf-repeater') ) {
             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 );

      Ensure that the field names (faq-acf-repeater, faq_question, faq_answer) match exactly with what you have set in ACF.

    3. Debugging: If you continue to receive a critical error, check your site’s error logs for more details on what might be causing the issue. This can provide insight into whether it’s a syntax error or a conflict with another plugin or theme.

    4. Shortcode Usage: After ensuring that the schema is correctly populated, you can use the shortcode provided by Rank Math to display the data on your site. Make sure to refer to the documentation for the correct shortcode usage.

    If you still encounter issues after following these steps, please reply to this ticket, and a human agent from the Rank Math Support team will assist you further. Thank you for raising this concern!

    As a note, please note that on line 4 of code base I’ve added “[” as this was missing from example from website. and then the file name “rank-math.php” doesn’t throw a critical error BUT the acf hook still doesn’t work. Thanks.

    Hello,

    Thank you for contacting Rank Math support.

    The code you are using isn’t correct. You haven’t mentioned the Schema type where you want to add the values. Here is a code to add the details to the Organization/LocalBusiness Schema:

    add_filter( 'rank_math/json_ld', function( $data, $jsonld ) {
        $data['publisher']['address'] = 
            '@type' => 'PostalAddress',
            'streetAddress' => esc_attr( get_field('street_address', 'option') ),
            'addressLocality' => esc_attr( get_field('locality', 'option') ),
            'addressRegion' => esc_attr( get_field('region', 'option') ),
            'postalCode' => esc_attr( get_field('postal_code', 'option') ),
            'addressCountry' => esc_attr( get_field('country', 'option') ),
        ];
        return $data;
    }, 10, 2 );

    Please note that the code will only modify the Schema in the front end. So the Schema shortcode won’t display the details you are adding using the code.

    Hope that helps and please do not hesitate to let us know if you need our assistance with anything else.

    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.

Viewing 3 replies - 1 through 3 (of 3 total)

The ticket ‘Integrate ACF into Schema’ is closed to new replies.