Skip to content

What are Hooks in WordPress and How to Use WordPress Hooks + Practical Examples

hooks in wordpress

What are Hooks in WordPress and How to Use WordPress Hooks In this tutorial, we will go over WordPress hooks and their purpose.. Furthermore, we will include practical WordPress hooks examples to show you how to use them on your site. What are Hooks in WordPress Using WordPress hooks is like having a superpower for your

What are Hooks in WordPress and How to Use WordPress Hooks + Practical Examples

Here at IT Doctorz we have written amazing and informative article for you How to Create Custom Meta Boxes in WordPress Posts and Post Types

What are Hooks in WordPress

WordPress Hooks lets you add extra skills and features to your site, helping you reach your business goals with ease.Imagine hooks as the friendly connectors in your website’s puzzle.

Hooks in WordPress with Examples

They let you play with and change the code in just the right spots, all without getting tangled up in the intricate web of the WordPress core. This makes it a breeze for folks like you to customize and sprinkle extra goodness into your WordPress plugins and themes, just the way you like it.

What is the Purpose of Hooks in WordPress

WordPress hooks allow users to manipulate WordPress without modifying its core. Learning about hooks is essential for any WordPress user as it helps them edit the default settings of themes or plugins and, ultimately, create new functions.

Types Of Hooks in WordPress

There are two types of Hooks in WordPress:

  • Action – allows users to add data or change how their websites work. An action hook will run at a specific time when the WordPress core, plugins, and themes are executing.
  • Filter – can change data during WordPress core, plugins, and theme execution.

How to Use WordPress Hooks

Using hooks requires a bit of HTML and PHP knowledge. Luckily, creating action and filter hooks is relatively easy, even for WordPress beginners.
Want to Learn HTML ?

Creating an Action Hook in WordPress

To add an action hook, you must activate the add_action () function in a WordPress plugin. To do so, add the following code to the functions.php file

Syntax for Action Hooks in WordPress

add_action( $target_hook, $the_name_of_function_you_want_to_use, $priority, $accepted_args );

Think of hooks as the traffic controllers of your website’s functions. They help determine the order in which different functions are executed. Imagine each function as a car, and the hooks decide which car goes first and which one goes last.
This decision is made using a priority scale, which is like a ranking system from 1 to 999. A lower priority value means a function goes early, while a higher one means it goes later. So, it’s like giving each car a number, and the cars with lower numbers get on the road first.
There’s also something called “$accepted_args,” which is like saying how many passengers can ride in each car (or function). By default, it’s set to 1, meaning one passenger (or argument). You can adjust this number based on what your function needs.
To give you an example in plain English, let’s say you’re customizing the Twenty Twenty-Three WordPress theme. You can use an action hook, which is like a special place in the code where you can add your own custom code. This way, you can make the theme do exactly what you want it to do, and you decide the order in which your custom code runs. It’s like adding your own twists to the theme’s story!

<?php
function hook_javascript() {
   ?>
       <script>
           alert('Hello world...');
       </script>
   <?php
}
add_action('wp_head', 'hook_javascript');
?>

Note the pattern in the example above:

  • <?php – the place where you put the hook to function.
  • function hook_javascript() – a function that will affect the initial value, thus showing the alert for users.
  • <script> – represents the text you want to display on the target_hook.
  • alert(‘Hello world…’); – will display an alert for users with the “Hello World” phrase.
  • add_action – the command for creating the action hook.

wp_head‘ – the target hook that the function will modify.

bNFCI5Oz8kYDxHVs2t ieaWvHrfQ5GS027ImA 7X43HQ9cIMwHnc5FmCiExsCir0CzTt5GD1S6z6BZpuI zq0rG2zQSghYmPBCRXHU70yWu lZf2rF5mWhVuBBWndJYsrPkbFEJYvveL9yRAQyPti8

Creating an Filter Hook in WordPress

​​You can create a filter hook by utilizing the add_filter() function. The filter hook modifies, filters, or replaces a value with a new one.
Here is an example of a filter hook that we will add to the functions.php file for the Twenty Twenty-Three WordPress theme:

add_filter( 'the_content', 'change_content' );
function change_content ( $content ) {
   $content = 'Filter hooks are amazing!';
   return $content;
}

imagine your website’s content is like a storybook. In this snippet:

  • ‘the_content’ is like a special page in the book we want to change.
  • ‘change_content’ is like the magic spell we’re casting to alter that page.
  • $content = ‘Filter hooks are amazing!’; is the new story we want to replace the old page with. It’s like saying, “Replace the original page with this cool new page that says ‘Filter hooks are amazing!'”

return $content; is like saving this new page into the book, making sure everyone sees it when they flip to that page.

So, when you open any post on your website using the Twenty Twenty-Three theme, instead of the usual content, you’ll see this exciting new page that says, “Filter hooks are amazing!” It’s like giving a surprise twist to every story in your book! 📖✨

563vnlWDvNfZjfdK2D3ZSPEIdfTAmT5ku83R0sBL3Ja9ljdO7wqFl2SnfIGbmGqWMtTRfHHOe7I7c77QVrO kHGLWuTSTB3n1GfYtCjGoT YqxdbOXM92yj CS

Practical Examples of Hooks in WordPress

There are a lot of hooks that users can use to create custom WordPress functions. Here are some of the most popular ones

admin_post_thumbnail_size

This filter hook displays a thumbnail image of your post in the Featured Image section. Three parameters connect the function: $size, $thumbnail_id, and $post.

$size = apply_filters( 'admin_post_thumbnail_size', $size, $thumbnail_id, $post );

Remember that you can change the $size parameter as you wish. For instance, if you want to set the thumbnail size to 240 x 240 pixels, utilize this code:

$size = apply_filters( 'admin_post_thumbnail_size', 240, $thumbnail_id, $post);

It is also possible to set a custom size for the thumbnail by adding the array () function. The code will look like this:

$size = apply_filters( 'admin_post_thumbnail_size', array(240, 400), $thumbnail_id, $post);

The array () function above sets the thumbnail to be displayed in 240 x 400 pixels.

after_password_reset

This action hook is activated when a user resets their password. The hook consists of two parameters, $user and $new_pass, and looks like this:

do_action( 'after_password_reset', $user, $new_pass );

For example, WordPress uses this hook with the reset_password() function.

Conclusion

WordPress hooks let you tweak how things work or stop certain actions without poking around the vital WordPress system files.

In this guide, we’ve made using these tools easy. It’s like showing you how to customize your car without getting into the complicated engine parts.

We really hope this guide is helpful. If you’re puzzled or curious, just drop a question below. We’re here to help you on this WordPress adventure! 🛠️💡

Scan the code