Filters and Hooks
We have incorporated various hooks and filters in the Rank Math SEO plugin that developers can use to interact with data output by Rank Math. You can control Rank Math data from within your themes/plugins. Before going through the different filters and hooks that Rank Math offers, we’d discuss how to add them to your website.
Adding Filters and Hooks in rank-math.php
We recommend adding these filters to wp-content/themes/theme-name/rank-math.php
as it involves benefits like:
Flexibility in Switching Themes
When you add code snippets to your theme’s functions.php, you run the risk of losing all your customizations added while switching themes, and it would be really tedious to identify all Rank Math code snippets used and add the same to your new theme.
By adding all Rank Math SEO code snippets in a separate file, you’ll have the flexibility to switch themes. All you’ve to do is download a copy of your rank-math.php file from your old theme and upload it to your new theme folder. Then, all your Rank Math customizations will work the same way as before.
Code Runs Only with the Plugin
When you add a filter to your theme’s functions.php, the code runs on your website even when the Rank Math plugin is not active. But when added to the rank-math.php file, the code runs only when the Rank Math plugin is active, as the plugin includes the file. This practice will help you ensure that your site runs as fast as possible.
Ensures All Rank Math Functions Work
Some filters like the one involving customizing OpenGraph need to be wrapped in the wp_head
hook. There are many reasons why they may not perform as intended when added to functions.php, and to work as expected, they need to be added to rank-math.php.
To keep things less complicated, we recommend adding all the Rank Math functions to rank-math.php instead of adding them to your theme’s functions.php.
How to Add Code Snippets to rank-math.php?
To start adding code snippets to rank-math.php, you’ll need to create the file first. You can easily do this with an FTP or cPanel File Manager.
Create rank-math.php File
If you’re using FTP, navigate to your theme’s folder (/wp-content/themes/theme-name/) and then create a new file named rank-math.php as shown below:
If you’re using cPanel, then you can use their File Manager to create your new file named rank-math.php inside your theme folder (/wp-content/themes/theme-name/) as shown below.
Navigate to Theme File Editor
Once you’ve created the Rank Math file in your theme folder, you can edit the file by navigating to Appearance → Theme File Editor (for Classic Theme) or Tools → Theme File Editor (for Block Theme) in your WordPress admin area.
The right side of your screen lists all the theme files and templates. The files being shown here would vary depending upon the theme you’re using on your website. You can access the rank-math.php file you had created in the previous step from the list of files.
Adding your Code
You’d be able to see the code editor available in the middle of your screen, and here is where you can add the code snippets (which we would discuss in this article shortly).
Saving Changes
Once you’ve added the code snippet, click the Update File button at the bottom of the screen to save your changes.
While adding your code snippet, if you’ve accidentally made any mistake that would crash your site, then the theme file editor will throw an error without saving the file so that you can fix them immediately. Once you’ve successfully updated the file, you can check your site for changes/configurations intended with the filter/hook.
Now that we’ve understood how to use the rank-math.php file, we’ll look at the different filters and hooks available for you to use.