WordPress Hooks – Everything You Need to Know
Here’s a question a lot of WordPress beginners might ask: what are WordPress Hooks, and what can they be used for?
Well, WordPress is a highly customizable CMS that allows its users to make changes according to their specifications. Some users customize their WordPress website using just the Appearance functionality while the rest add their custom functionality by using WordPress Hooks.
Table of Content
In this guide, I will explain what hooks are in WordPress and how they can be used to modify or create a specific functionality in WordPress.
What are WordPress Hooks?
WordPress Codex defines hooks as a bridge through which WordPress themes and plugins interact with the WordPress core. The functionality of hooks allows the developers to connect their custom code with the default functions present inside the WordPress core.
It’s safe to say that hooks are custom PHP functions that can be used or hooked to alter the default WordPress behaviour without affecting the core files. Hooks allow WordPress to interact with the custom code present inside plugins and functions.php file.
There are two types of hooks; Action Hooks and Filters Hooks.
Action Hooks
Action hooks are executed at a specific time to perform a certain task like displaying an output or adding data to the database. Action hooks do not return any value and they only perform a certain task when called upon.
For example; create_category function runs whenever a new category is created and does not return anything.
Filters Hooks
Filters Hooks, on the other hand, allow you to change the data during the execution of WordPress. Functions hooked to Filters accept variables and return a value. Some Filters also accept more than one argument.
For example; add_filter(‘the_title’, ‘strrev’); accepts the string as an argument and returns the reverse of that string.
Filters are used to handle a specific task and they can modify the data and always return something at the end.
How WordPress Hooks Are Used?
Now that we have understood the basic concepts of WordPress hooks, let’s move ahead and see both Action and Filters in action.
Action Hooks Example
There are more than 200 hooks available so covering them all is not a good idea. However, to get you started, I’m going to show you a simple example and then I’ll explain what I’ve done.
Example 1: Custom Menu in the Admin Area
1 2 3 4 |
function custom_menu() { add_menu_page( 'custom menu title', 'custom menu', 'manage_options', 'testplugin/testplugin-admin.php', '', 'dashicons-admin-site', 6 ); } add_action( 'admin_menu', 'custom_menu' ); |
Let’s examine the code above by going line-by-line.
In the first line of the code, I gave a name to my function. In the second line, I am calling another function with a few arguments inside. Finally, I attached my custom function with an Action hook of ‘admin_menu’ which will run my function whenever the admin menu is being generated.
Example 2: Adding Custom Text After the Header
1 2 3 4 5 6 7 8 |
// Add custom text after the header function add_custom_text() { if ( !is_front_page() ) return; // Echo the html echo "<div>Avail the special summer discount 20%</div>"; } add_action( '__after_header' , 'add_custom_text' ); |
In the code above, if the current page is not the home page then the function won’t display the custom text. Which basically means that the custom text will only show on your website’s homepage.
At the end of the code snippet, I attached my custom code to an Action hook ‘__after_header’.
Similarly, you can experiment with other Action hooks and create your custom functionality.
Filter Hooks Example
Unlike Action hooks, Filters accept arguments and return a value. Let’s look at the examples and see how they can be used.
Example 1: Changing the Excerpt Length
1 2 3 4 |
function excerpt_length_example( $words ) { return 15; } add_filter( 'excerpt_length', 'excerpt_length_example' ); |
In the code above, I take advantage of one of the WordPress Filters ‘excerpt_length’ and hook it with my custom code which returns the numeric value of 15 for my new excerpt length.
Example 2: Change Title
1 2 3 4 5 |
// Change title function change_title($title) { return 'Hooked: '.$title; } add_filter( 'the_title', 'change_title' ); |
In this simple example, I simply defined a custom function for changing the post title. Later, I hooked it to a filter ‘the_title’.
Wrapping It Up!
WordPress hooks are useful in connecting custom functionality with your WordPress’s core features. In this guide, we saw how Action and Filter hooks are different and how they can be used at various places inside our WordPress sites.
If you have any questions, or if you’re confused about any concept in the article, let me know in the comments section below and I’ll get back to you!
Create Faster WordPress Websites!
Free eBook on WordPress Performance right in your inbox.