Hello,
Thank you for reaching out to us. To use your custom-made shortcode in the Rank Math title tag, you need to follow these steps:
-
Add the Code Snippet:
- Go to the Theme File Editor and choose the
rank-math.php
file.
- Add the following code snippet to register your custom variable:
/**
* 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',
array(
'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'
);
});
function custom_variable_callback()
{
// Your custom logic to return the number of products reviewed
return 5; // Example: Return 5 for testing
}
-
Update the File:
- Click the Update File button at the bottom of the screen.
After following these steps, you should be able to use your custom-made shortcode within the Rank Math title tag successfully. If you encounter any issues or need further assistance, please feel free to ask.
If you have any more questions or need additional help, please don’t hesitate to ask.
Thank you!
Sources:
Product Schema Type » Rank Math
Set the Current Year in Post Title Using the [year] Shortcode » Rank Math
Using Rank Math’s Local SEO Module » Rank Math
Using Variables in the SEO Title and Description » Rank Math
Hi again,
I made it work, thanks. However, how can i keep my new rank-math.php file when I update the plugin?
Hello,
The rank-math.php file won’t be affected by your theme or Rank Math plugin updates.
Looking forward to helping you.
Well, I just updated RankMath, and everything was lost. So thank you. It’s not true that the rank-math.php file is not touched upon updating the plugin…
Hello,
Sorry for that.
Please note that the rank-math.php
file is supposed to be saved in the theme file, so any changes to our plugin shouldn’t cause the file to be deleted.
It depends on the theme if they can exclude removing any custom files within their folders so you can check that with your theme author.
We can suggest checking out different other methods on adding custom filter code to your website: https://rankmath.com/kb/wordpress-hooks-actions-filters/
We also recommend the Code snippet plugin as it ensures the code is not affected upon plugin/theme updates.
Hope that helps.
I already have the Code Snippets plugin (https://codesnippets.pro/) installed, and I use that for functions within functions.php. But I don’t know how I can use the plugin to insert code into rank-math.php? How can I do that?
Hello,
You do not need to use the rank-math.php
file when using the Code Snippet plugin.
You can use either the Code Snippet plugin or the rank-math.php
to add your code snippet. Using either of them will work.
We hope that helps you.
Thank you.
So, for the code that’s supposed to be inserted in rank-math.php: You’re saying that I can easily just add that code as a snippet to the Code Snippets plugin, and then it will work?
Hello,
Yes, any code that works in your theme’s function.php
file will also work via the Code Snippets plugin.
Just add the code and let us know if you have any issues.
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.