You are Here:Home>>Code>>WordPress>>Begginer’s Guide to WordPress Actions and Filters

Begginer’s Guide to WordPress Actions and Filters

Ivan
By | 2017-09-07T12:44:44+00:00 Jul 10, 2017|WordPress|

Even if you are not a developer, but have some experience with WordPress, you must have heard of WordPress hooks, this is, WordPress actions and filters. This term is tightly connected to the development of WordPress and is something that you will need to understand if you want to start working on your own themes and plugins. Understanding hooks will also greatly help you if you just want to modify a few specific things on your own blog.

Although there are tons of plugins that can help you customize WordPress, there will be times when a plugin just won’t do. And you will have to manually change the code to achieve something on the blog. Changes to HTML and CSS code are just part of the modification process. Now it’s time to dig a bit deeper and learn what’s behind the mask of WordPress.

Before you get scared away by the term or the fact that it has to do something with PHP and programming in general, let us show you that WordPress hooks are quite simple to start with.

If you are new to WordPress you might want to check first the beginners series articles on WordPress, especially the article about working with Child Themes in WordPress.

What are WordPress hooks?

WordPress hooks are actually the WordPress actions and filters. To be more precise, hooks are pieces of PHP code that are wrapped in do_action() or apply_filters() functions. They allow developers to enhance and customize a WordPress site in any imaginable way.

By utilizing WordPress hooks, you can take a function from one place and execute it on another one without having those pieces of code necessarily one next to another.

Now when you know that, it is easier to understand how plugins and themes can easily add (actions) or modify (filters) different aspects of the entire WordPress sites just by hooking to a specific code used by WordPress. The more you think about it, the more amazing it sounds, doesn’t it?

What are WordPress actions and filters

Actions and filters have a very similar structure. To a beginner, they will look pretty much the same. If you just happen to copy and paste a WordPress hook from the Internet in order to achieve a specific result on the site, it won’t really matter which is which. But the truth is that they work quite differently, and if you want to use them on your own, you will have to know the differences.

WordPress actions

function custom_admin_menu() {
add_menu_page( 'custom menu title', 'custom menu', 'manage_options', 'myplugin/myplugin-admin.php', '', 'dashicons-admin-site', 6 );
}
add_action( 'admin_menu', 'custom_admin_menu' );

Actions are pieces of code that add value to your site. When a page is loading, WordPress works all kinds of queries to the database and loads whatever it’s instructed to. When it finds a hooked action, WordPress will simply execute that function and add it into the loading sequence, in addition to everything else. An action is triggered at a specific time during the page load, and it won’t modify existing functions.

For example, one of the first things that WordPress loads is the header. So if you wanted to add some functionality to the header of your website, you would use an action and hook (attach) it to the header function. So, after WordPress opens any of your pages, it would start to load header and detect that the header part has a new action which should be loaded in addition to all others that might have already been there. That can be displaying text, menus, calculating something or actually anything that you want to do.

To learn more about specific WordPress actions, visit the official Codex page.

WordPress filters

Unlike actions, WordPress filters are used when you want to modify something on the site. Instead of just adding another functionality, filters take existing ones and alter their behavior before WordPress gets it back to the database or user’s browser.

For example, you can add a WordPress filter that will find titles of your posts before they are displayed and change them in some way. So instead of showing a regular title, you can tell WordPress always to add, let’s say, a date in front of it. Since the title is already being loaded, a WordPress filter would intercept the function, alter it with the code you provided and then load back the function before it gets displayed to the user. This way, WordPress would always go through your filter before showing the end result.

To learn more about specific WordPress filter, visit the official Codex page.

The structure of WordPress hooks

Before starting to write your own WordPress actions and filters, you should know how do they look. There are few things you should have in mind when dealing with the structure of hooks.

add_action ( ‘hook_name’, ‘custom_function_name’, [priority], [accepted_parameters] );

function custom_function_name() {
// code goes here
}
  1. hook_name
    This is the name (location) of the actual hook that you’re trying to connect to. For example, wp_head. See the official Codex pages to find the list of all standard WordPress hooks you can use.

  2. custom_function_name
    Whether you’re writing your own one or using a standard WordPress function, this is the place to let the platform know what code to attach to the hook you specified in the previous step.

If you’re using an existing function, make sure that you write it down correctly. If you are writing your own, give it a custom name. The function has to be unique and consist of letters, numbers and underscores only.

  1. Priority
    A priority argument is a number between 1 and 999 where default number is 10. It tells WordPress in which order to execute functions (if there is more than one function hooked). In some specific cases, it will be important to implement a particular function before another one, and this will be the place to instruct WordPress on that.

  2. Accepted parameters
    If your function returns more than one parameter, you must define the number of available parameters here.

Examples

Now that you know what WordPress actions and filter are and what the structure looks like let’s see some examples.

WordPress action

In the following example, we are going to add action every time that WordPress generates an admin menu. This is usually used when developers want to add custom menu items for their themes and plugins.

function custom_admin_menu() {
add_menu_page( 'custom menu title', 'custom menu', 'manage_options', 'myplugin/myplugin-admin.php', '', 'dashicons-admin-site', 6 );
}
add_action( 'admin_menu', 'custom_admin_menu' );

WordPress filter

Now let’s try to change the length of an excerpt that WordPress shows on your site. By now, you should have recognized that for changing something you will need to use a filter instead of an action.

function new_excerpt_length( $words ) {
return 20;
}
add_filter( 'excerpt_length', 'new_excerpt_length' );

Where to use actions and filters

Since WordPress hooks are used in order to avoid hard coding functions in specific files, you don’t have to find those particular locations to write them down. Usually, WordPress actions and filters are added directly to functions.php file of an active theme.

If you want to avoid problems when updating themes, we suggest that you create a child theme where you can safely store all of your custom actions and filters. Also, it is possible to create a custom plugin for WordPress so that you can more easily turn on and off the specific features that you’ve added to the site in the form of WordPress hooks.

How to remove hooks

So far, we covered the very basics of adding actions and filter to your WordPress site. But what if you wanted to remove specific hooks that you don’t need?

Don’t worry; you will just need one line of code.

remove_action( $tag, $function_to_remove, $priority );
remove_filter( $tag, $function_to_remove, $priority );

Adding priority here is optional. But in some cases, you will want to remove only the specific occurrence of that hook. To do that, it will be important to know its priority.

Conclusion

As you can see, WordPress hooks aren’t that hard to understand. If you give them a chance, you will quickly learn how WordPress works, and it will be much easier to work on your own customizations. By understanding the structure of WordPress actiWordPressilters, it will be much easier to modify the existing functions and alter the code that you find on the Internet.

But this is just the first step. If you’re serious about WordPress development, there’s much more to learn and understand about WordPress actions, filters, and functions. But don’t give up. It’s a fun and rewarding world of WordPress development.

Have you enjoyed this tutorial? If so, supports us by sharing this article or Become a Patron!

About the Author:

Ivan
I am a WordPress enthusiast and a power-user who loves to work and write about the platform at WP Loop. I work as a digital marketer and I'm also a growth hacker wanna be at Web Factory Ltd.