Custom variable preview not working in Meta Box Snippet Editor preview

#462698
  • Resolved John Christensen
    Rank Math business

    I’m hoping I can get some help making my custom variable previewable in the Meta Box Snippet Editor preview. The custom variable is working in the frontend. (But I need it to work in the backend too).

    Here’s my code:

    add_action( 'rank_math/vars/register_extra_replacements', function(){
    rank_math_register_var_replacement(
    'op_shortcode',
    [
    'name' => esc_html__( 'OP ACF Field', 'rank-math' ),
    'description' => esc_html__( 'Custom ACF field from ACF shortcodes.', 'rank-math' ),
    'variable' => 'op_shortcode(field-name)',
    'example' => esc_html__( 'Custom field value', 'rank-math' ),
    ],
    'get_op_shortcode_callback'
    );
    });
    function get_op_shortcode_callback( $name, $post_id ) {

    global $post;
    $post_id = "option"; // options page
    $name = get_field( $name, $post_id, true );
    return '' !== $name ? $name : null;
    }

    Screenshot of code: https://ibb.co/7CFqQK9

    This says that it should work in the Meta Box Snippet Editor here: https://rankmath.com/kb/filters-hooks-api-developer/#add-extra-variables

    This is what I’m seeing in the Meta Box Snippet Editor: https://ibb.co/YcBvRZY

    Any help is appreciated!

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

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

    There’s no additional code to be added to the filter since the custom variable is already working on the front end. For the preview to work, you need to ensure that the current content editor you are working on has WordPress editor support.

    Hope that helps.

    Thank you.

    I’m confused because the other customfield(field-name) variable IS working. So what am I missing? Sorry if I’m not understanding.

    Screenshots here for reference:

    https://ibb.co/9VDvhT3
    https://ibb.co/pJtjf5t
    https://ibb.co/tsJwzfY (Both loading on frontend)

    Thank you!

    Hello,

    When this happens it means that the custom variable you created contains code that cannot be rendered in the backend because the data is still not yet available.

    The variable %customfield(field-name)% works because it’s built from the ground up to load in the backend.

    Don’t hesitate to get in touch if you have any other questions.

    Do you have any insight on how to get it to load in the backend? Or is it too complicated to explain? Just trying to gain some hint/idea on how to get this to work. I understand if you can’t explain but just looking for a straightforward answer.

    Thank you!

    Nigel
    Rank Math business

    Hello,

    The following article explains the default WordPress hooks firing sequence: http://rachievee.com/the-wordpress-hooks-firing-sequence/. Beyond that article, you will have to research further into how to get the variable to load. ACF support/forums might be able to help.

    Hope that helps. Please let us know if you have questions.

    Okay, thank you! I appreciate your help.

    Anas
    Rank Math business

    Hello,

    If you have any questions in the future, please feel free to create a new forum topic, and it will be our pleasure to assist you.

    Thank you.

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

The ticket ‘Custom variable preview not working in Meta Box Snippet Editor preview’ is closed to new replies.