Custom Variables

#493264
  • Resolved Xaibi
    Rank Math free

    Hello,
    I need to custom variables but can’t understand how I can add them.

    I see this code but where to add 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(
    'custom_variable_slug',
    [
    'name' => esc_html__( 'Custom variable name.', 'rank-math' ),
    'description' => esc_html__( 'Custom variable description.', 'rank-math' ),
    'variable' => 'custom_variable_slug',
    'example' => 'custom_variable_callback()',
    ],
    'custom_variable_callback()'
    );
    });

    I am using this code to display data

    <?php echo $row->name_english ?>
    <?php echo $row->name_gender ?>
    <?php echo $row->name_origin ?>
    <?php echo $row->name_religion ?>

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

    Thanks for contacting us and sorry for the inconvenience caused.

    You will have to create a function to get the data from the field you want and add it to our plugin’s field using the variable. Here is an example of how to create a variable:

    add_action( 'rank_math/vars/register_extra_replacements', function(){
    	rank_math_register_var_replacement(
    		'woo_price',
    		[
    		'name'        => esc_html__( 'woo_price', 'rank-math' ),
    		'description' => esc_html__( 'WooCommerce Price', 'rank-math' ),
    		'variable'    => 'woo_price',
    		'example'     => 'woo_price_callback()',
    		],
    		'woo_price_callback'
    	);
    });
    
    function woo_price_callback() {
    	if(is_product()) {
    		global $product;
    		return $product->get_price();
    	}
    }

    This filter creates a variable named %woo_price% that gets the product’s price. If you want more help, please share more details about the fields you want to get the data from so we can help you further.

    And you can follow this guide to add a filter to your site: https://rankmath.com/kb/wordpress-hooks-actions-filters/

    Looking forward to helping you.

    Xaibi
    Rank Math free

    ok, I created a custom table inside the WordPress database and fetch data from there with this code.

    <?php
    global $wpdb;
    $name = get_query_var('christian_girl_name');
    $sql = $wpdb->prepare("SELECT * FROM wp_names_christian WHERE name_english=%s;", $name );
    $row = $wpdb->get_row( $sql );
    ?>

    And I have 12 fields to show data like this…

    <?php echo $row->name_english ?>
    <?php echo $row->name_gender ?>
    <?php echo $row->name_origin ?>
    <?php echo $row->name_religion ?>

    And so on… so how I can show them in the description and title.

    Hello,

    Please refer to this example custom variable instead:

    add_action( 'rank_math/vars/register_extra_replacements', function(){
    	rank_math_register_var_replacement(
    	   'shortcode_var_name', [
    	   'name'        => esc_html__( 'Author name', 'rank-math' ),
    	   'description' => esc_html__( 'Author description', 'rank-math' ),
    	   'variable'    => 'shortcode_var_name', /* the actual variable: %shortcode_var_name% */
    	   'example'     => shortcode_var_name_call_back(),
    	   ],
    	   'shortcode_var_name_call_back'
       );
    });
    function shortcode_var_name_call_back(){
    	return $row->name_english;
    }

    After applying the code, you should be able to use the variable %shortcode_var_name% in our meta title/description.

    You should see that the callback function shortcode_var_name_call_back returns the value you wish for that variable to display.

    You can do the same with the other $row->*.

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

    Xaibi
    Rank Math free

    Thankyou for this, I have a question if I want to show 12 fields then I have to do copy and paste 12 times in rank-math.php?

    Xaibi
    Rank Math free

    Also, I try your code but I am getting an empty value.

    add_action( 'rank_math/vars/register_extra_replacements', function(){
    	rank_math_register_var_replacement(
    	   'shortcode_var_name', [
    	   'name'        => esc_html__( 'Name Meaning', 'rank-math' ),
    	   'description' => esc_html__( 'Show the Name meaning', 'rank-math' ),
    	   'variable'    => 'shortcode_var_name', /* the actual variable: %shortcode_var_name% */
    	   'example'     => shortcode_var_name_call_back(),
    	   ],
    	   'shortcode_var_name_call_back'
       );
    });
    function shortcode_var_name_call_back(){
    	return $row->name_meaning;
    }

    Hello,

    Yes, you would need to create the variable code snippet for each one giving them all different names in the variable and callback entries.

    As for the issue of this not showing data, please note that since your code only runs on the frontend this won’t appear in the snippet editor in the backend so you should check in the frontend on pages where you have this variable if the data is coming through.

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

    Xaibi
    Rank Math free

    ok i got it.. thanks…

    Hello,

    Glad that helped.

    Please feel free to reach out to us again in case you need any other assistance.

    We are here to help.

    Thank you.

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

The ticket ‘Custom Variables’ is closed to new replies.