Using ACF Field in schema

#496037
  • Resolved Tom
    Rank Math free

    Hi there,

    Really hoping you can help with this. To preface, I am not very proficient in programming so forgive me if my attempt looks poor.

    I have an ACF field that I named “your_reward”. The field type is a text field. I am trying to make it so all my posts display the information in this field by default in RankMaths single post title.

    I tried following your steps at this link and ended up with the following code:


    /**
    * Action: 'rank_math/vars/register_extra_replacements' - Allows adding extra variables.
    */add_action( 'rank_math/vars/register_extra_replacements', function(){
    rank_math_register_var_replacement(
    'your_reward',
    [
    'name' => esc_html__( 'Your Reward.', 'rank-math' ),
    'description' => esc_html__( 'Shows Reward For Current Post.', 'rank-math' ),
    'variable' => 'your_reward',
    'example' => 'your_reward()',
    ],
    'your_reward()'
    );
    });

    Whilst this is showing the variable in the dropdown menu, it’s not pulling the data from the ACF field, so I’m doing something wrong. I’d imagine the solution is rather obvious, but due to my limited knowledge I’m not able to figure it out.

    I’d really appreciate your help.

    Many thanks,
    Tom

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

    Thank you for contacting Rank Math support, and sorry for any inconvenience that might have been caused due to that.

    The callback function isn’t returning anything, which is why this code isn’t working so your code isn’t really doing anything.

    Kindly refer to this code instead:

    add_action( 'rank_math/vars/register_extra_replacements', function(){
    	rank_math_register_var_replacement(
    	   'your_reward', [
    	   'name'        => esc_html__( 'Author name', 'rank-math' ),
    	   'description' => esc_html__( 'Author description', 'rank-math' ),
    	   'variable'    => 'your_reward', /* the actual variable: %your_reward% */
    	   'example'     => your_reward_call_back(),
    	   ],
    	   'your_reward_call_back'
       );
    });
    function your_reward_call_back(){
    	global $post;
    	return get_field('your_reward', $post->ID);
    }

    Then, make use of this variable %your_reward% in your meta fields.

    Hope that helps.

    Thank you.

    Tom
    Rank Math free

    This works perfectly, thank you so much for the kind reply – You’ve made my day!

    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.

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

The ticket ‘Using ACF Field in schema’ is closed to new replies.