1. Home
  2. Knowledge Base
  3. How to Add a Filter/Hook to Your Website?

How to Add a Filter/Hook to Your Website?

Filters and hooks are functions that can be added to your WordPress website allowing developers to make any changes to default WordPress functionality. Rank Math also offers various filters and hooks for developers making it incredibly easy to interact and modify how Rank Math works…

In this tutorial, we’ll share with you two different methods to add a filter/hook to your website.

Method 1: Using Code Snippets Plugin

The first method is by using a plugin called Code Snippets. Using this plugin, you can add custom snippets to your website’s functions.php file.

1 Install Code Snippets Plugin

To get started, you need to install the Code Snippets plugin on your website. Head over to your WordPress dashboard and navigate to Plugins > Add New > Code Snippets. Install the plugin and activate it.

2 Adding New Code Snippet

To add a new filter/hook, navigate to WordPress Dashboard > Snippets > Add New.

3 Adding Title and Code

Enter a title for your snippet and enter the code snippet with your filter/hook in the respective fields.

4 Choose the Settings

You can also notice some settings are available on the same page, which will help you configure when and where the code snippet should appear in your functions.php.

Run Snippet Everywhere

If you want this filter/hook to run all the pages (both front-end and back-end) of your website, choose Run Snippet Everywhere.

Only Run in Administration area

When you choose Only run in administration area, the filter/hook will run only in the WordPress admin area and will not run in the front-end of your website.

Only Run on Site front-End

If you wish to run this filter/hook only on your website’s front-end and not anywhere in the WordPress admin area, choose this option.

Only Run Once

If you wish to use your filter/hook to update a setting or activate a cron job or anything else that needs to be done only once, then choose this setting.

Priority

The default priority of this filter/hook will be 10. However, if you have multiple filters for a specific hook and need to prioritize which runs ahead/behind, you can use the priority.

5 Description and Tags

Although adding a description and tags for your code snippet is optional, it is a good practice to have them, so that, if another developers wants to make some changes to your code, it would be easy to understand the purpose of adding the specific code.

6 Save Changes and Activate

By clicking Save Changes and Activate, the code filter/hook will be saved and activated — that would immediately reflect in your website’s functions.php file. However, if you decide to save changes but not to implement them, then click Save Changes.

7 Removing a Filter/Hook

Adding or removing a filter/hook is easy — just navigate to WordPress Dashboard > Snippets > All Snippets and toggle the code snippet to add or remove it from the functions.php file

Method 2: Add the Code to Your Theme’s functions.php

The second way to add a filter/hook to your theme’s functions.php would be to directly edit the functions.php file.

Caution: We recommend you use a child theme and edit the functions.php of your child theme instead of the parent theme, as you would lose the modified code and changes done when you update your theme, which might cause some adverse effects on your website.

1 Navigate to Theme Editor

To edit your theme’s functions.php navigate to WordPress Dashboard > Appearance > Theme Editor.

WordPress - Appearance - Theme Editor

2 Edit Your functions.php File

Choose the functions.php file from the right sidebar, that lists down all the files in your child theme. Add your new filter/hook to the file content and click Update File for the changes to take effect.

Add your filter/hook to your theme's function.php

And, that’s it! We hope this tutorial was helpful in adding filters and hooks to your WordPress site and if you have any questions on using Rank Math’s filters and hooks, feel free to reach our support team – we’re always more than happy to help.

Was this article helpful?

Still need help?

?

Submit Your Question

Please give us the details, our support team will get back to you.

Open Ticket