Fill out event schema from ACF fields

#758647
  • Resolved Ehv
    Rank Math pro
    Content AI Starter

    Hi there,

    I know there are a lot of information about ACF repeater fields and Rank Math.
    But in this case I would like to use your event schema automaticly with values from an ACF field.
    Let’s say I have the event starting date and I want to pull the value from a ACF date-/time picker field.
    How could I do this?

    The second question:It’s an online event and the url always corresponds to the page the user is currently on. Is there a way to have the current url filled in automatically?

    Best regards!
    Niko

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

    Thank you for reaching out to us with your questions about using ACF fields in the event schema and automatically filling in the current URL for an online event.

    To pull the starting date of the event from an ACF date/time picker field, you can achieve this by defining a new variable using a filter to get the value from the ACF field and use it in the Schema Generator. Here’s an example of how you can do this:

    1. Register the ACF variable in Rank Math by adding code similar to how WooCommerce variables are registered in the Rank Math plugin. You can find an example of registering a WooCommerce variable in the Rank Math plugin here.

    2. Use the rank_math/json_ld filter to generate the event schema on the front end instead of adding it manually from the Schema generator. You can add the following code snippet to your theme’s functions.php or rank-math.php file:

    add_filter( 'rank_math/json_ld', function( $data, $jsonld ) {
        $start_date = get_field('your_acf_start_date_field_name');
    
        $data['startDate'] = $start_date; // Assign the ACF start date to the startDate field in the JSON-LD data
    
        return $data;
    }, 10, 2 );

    Regarding automatically filling in the current URL for an online event, you can achieve this by dynamically populating the Online Event URL field with the current page URL. This can be done using JavaScript or PHP to fetch and insert the current URL into the field.

    If you need further assistance or have any more questions, please feel free to ask.

    Best regards!

    Sources:
    How to Use ACF Repeater Fields to Automate FAQ Schema » Rank Math
    Event Schema Type » Rank Math
    How to Use ACF Fields in Schema Generator » Rank Math
    Configuring “Schema Markup” in Rank Math » Rank Math

    Ehv
    Rank Math pro
    Content AI Starter

    Thanks! So both code snippets you have mentioned (in step 1 and 2) should be added to the function.php, right?

    Hello,

    Thank you for your patience.

    Yes, you can add the filter to your theme or child theme’s functions.php file.

    Here are other ways you can follow to add a filter on your site:
    https://rankmath.com/kb/wordpress-hooks-actions-filters/

    Looking forward to helping you.

    Ehv
    Rank Math pro
    Content AI Starter

    Hi Reinelle,

    I would like to create events with ACF fields like the start date or the event description. I do not wand do care about SEO or RankMath at all for these events. This means, if I create a new event and fill out the ACF fields ALL fields from your event schema should be filled out automaticly. Is this what your script is doing?

    One more thing: Some of my event have two or even three event dates. For these events I have i.e. three ACF date picker fields. (start-date01, start-date02, start-date03). Could I add three event schema with the pro version of rank math and let them be filled out automatically?

    If you have an example script for these issuu, I would be very thankfull!

    Thank you!
    Niko

    Hello,

    Since you are simply using ACF fields for your event information, you can simply use this variable %customfield(FIELD_NAME)% and insert it into the schema fields.

    With schema PRO you can fully customize the schema structure to your liking, however, you also need to make sure that it is always valid by checking it here.

    As for the Event schema’s startDate, having multiple of that property may cause an invalidation.

    Looking forward to helping you.

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

The ticket ‘Fill out event schema from ACF fields’ is closed to new replies.