If you are a WordPress Developer or want to learn about WordPress to develop a Plugin or Theme, then you should get to know about WordPress hooks.
If you do not want to become a complete developer, but if you want to change something with programming on the site, then you must also know the WordPress hooks.
In this article, we will learn the following topics:
- What are Hooks
- Action Hooks
- Filter Hooks
- Removing Filter and Action Hooks
- Different between action and filter hooks
What are WordPress Hooks
A whole WordPress site is made up of functions and database query, the theme and plugin are working together with WordPress to output the text, images, styles, and other files. All of those are interpreted by the browser and put together into a single web page.
The hook is a generic term in WordPress which is used to change the core functionality of WordPress and Plugins without touching the core files and you can write your own hook.
There are two types of hooks in WordPress: Action and Filter.
Action Hook
Action hooks let you perform the custom functions called actions.
It allows you to add extra functionality to the WordPress core and the theme. You can achieve by writing your own function in the action hook.
The following is the syntax for writing action hook:
1 | add_action( 'action_hook', 'your_function', 'priority', args ); |
Suppose you are deleting the user and want to send an email as notification that “your account has been deleted”. For this, you can apply an action hook.
The following are the steps for applying the action hook:
- First, open function.php of your activated theme which will be located on the “/wp-content/themes/your-theme” path.
- Add the following lines of the code into the function.php. If mail set up is working on your server then it will send an email to the user.
12345678add_action('delete_user', 'send_email_before_delete_user');function send_email_before_delete_user($user_id){$email = get_user_option('user_email', $user_id);$subject = 'You account has been deleted';$message = 'The content of the email.';wp_mail($email, $subject, $message);}
For your reference, you can check the link.
Filter Hook
Filter hooks are also WordPress hooks that manipulate the text and other output.
Suppose you want to add social media links to the end of all posts and if the user clicks on that social media link then that page becomes open.
- First, you have to open function.php of your running theme which will be located on “/wp-content/themes/your-theme”.
- Add the following lines of code into the function.php:
1234567add_filter('the_content', 'social_icons_after_post_content');function social_icons_after_post_content($content){if (is_single()) {$content .= '<ul><li><a target="_blank" href="https://twitter.com/troposal/">Twitter</a></li><li><a target="_blank" href="https://www.facebook.com/troposal/">Facebook</a></li></ul>';}return $content;}
For your reference, you can check the link.
Removing Filter and Action Hooks
It is very easy to remove the hooks that you have attached to WordPress. It is the opposite way to add hooks, which has a very similar syntax:
1 2 | remove_action( 'action_hook', 'function_to_remove', 'priority'); remove_filter( 'filter_hook', 'function_to_remove', 'priority'); |
The “priority” argument is optional.
Different between action and filter hooks
Action Hook | Filter Hook |
---|---|
Getting new things done. Ex. adding CSS and JS. | It works on that are already in WordPress. Some things to add or remove. Suppose you add text after the post content. |
An Action hook does not need to return value. | It is required to return a value. |
An Action hook does not need to return value. | The hooked function gets a specific value. It does |