variable acf in the seo title

#270004
  • Resolved PriscillaITfy
    Rank Math free

    Hello,

    I have an ACF field that I would like to add to the meta title of a taxonomy archive dynamically.
    I have reinterpreted a piece of code I found in the Rank Math support answers.
    The new variable was indeed created in the Rank Math drop-down menu: %title_h1%.
    But it returns –> title_h1_callback() | (sitename)

    Here is my 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(
    'titre_h1',
    [
    'name' => esc_html__('titre champs personnalisé ACF', 'rank-math'),
    'description' => esc_html__('titre h1 de la page du tag', 'rank-math'),
    'variable' => 'titre_h1',
    'example' => 'titre_h1_callback()',
    ],
    'titre_h1_callback()'
    );
    });

    function titre_h1_callback()
    {
    return 'titre_h1';
    }

    I’m probably missing something simple, but if you could tell me where I’m going wrong, that would be great.

    Best,

    Priscilla

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

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

    You are calling the callback function as string so it surely won’t work. I have fixed the code for you. Please refer to this code:

    add_action('rank_math/vars/register_extra_replacements', function () {
        rank_math_register_var_replacement(
            'titre_h1',
            [
                'name' => esc_html__('titre champs personnalisé ACF', 'rank-math'),
                'description' => esc_html__('titre h1 de la page du tag', 'rank-math'),
                'variable' => 'titre_h1',
                'example' => titre_h1_callback(),
            ],
            'titre_h1_callback'
        );
    });
    
    function titre_h1_callback() {
        return 'titre_h1';
    }

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

    Hello Jeremy!

    Thanks for your feedback.

    I have indeed changed the code in this sense.

    As you can see here: https://ibb.co/j64GMnq, I can get the %title_h1% variable in the Rank Math dropdown menu.
    But it returns “Example”, I don’t understand why?

    It should return the title of my taxonomy page that I created in ACF (which is not example)

    Here is my code for clarity:
    https://ibb.co/86hVmhd

    I think it is my return function that is not correct, but I don’t see what?

    Would you have a solution for me?

    Thank you for your help!

    Hello,

    It will show the Example in the preview when you see it in the Titles & Meta settings.

    Please check in one of your posts, it should work fine.

    Let us know how that goes. Looking forward to your update.

    Thanks.

    Hello,

    Thanks for your quick feedback!

    Great! That means I’m on the right track 🙂

    But I still have a doubt… when I do the test in the Meta title of a label –> https://ibb.co/LvHhrft

    “Example” is always displayed while the value of my custom field is “Hybrid application”, it should be displayed, right?

    If I understand correctly, it is normal that the word “Example” is displayed?

    The site is in pre-production, it is not yet indexed in the eyes of Google, I can’t really check.

    Is there another way to check?

    Thank you very much for your precious help!

    If I understand correctly, the preview returns “Example” because the object does not fill in and the “Example” is there by default.

    Because when I test my variable $title_h1 it returns the value of my field: string(19) “Hybrid application” string(19) “Hybrid application”

    Please confirm if this is correct 😀

    Hello,

    Reviewing your code, it should return the desired value from your ACF field. Could you please check the website’s page source and see if the correct meta title is displayed there?

    Let us know how it goes. If there’s an issue, do let us know as well and we will further help you fix it.

    Thank you.

    Hello,

    It’s perfect, it works, thank you very much!
    I had not thought at all to look in the source code of the page.

    Thank you for your insights 🙂

    The issue is resolved.

    Tammy
    Rank Math business

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

You must be logged in to reply to this ticket.