A Beginners Guide to WordPress Hooks


WordPress-hooks

If you are already in WordPress plugin development or theme development, then you should already know what WordPress “Hooks” are.

However, as you have found this post, I’m assuming that you have a brief idea about it.

Here is the thing:

Even if you are a beginner in “WordPress” or a Professional, you should get yourself familiar with the term “Hook”.

Interestingly, whether Knowingly and unknowingly, every WordPress theme or plugin developer is found working on WordPress hooks.

As a proud WordPress user, I can say that WP has really made the life of developers easy. If you understand PHP and wp query, you can easily modify the behaviour of a plugin or a theme.

So, what is this hook all about?

Well, hook allows us to make those prominent changes to your theme and plugins. Hooks allow you to modify WordPress functionality without editing the core files as they hook the custom function to an existing function.

Knowing the Term “HOOKS”

Generally, a hook in WordPress is the certain location where you can add your own code and run it, absolutely without modifying the core files. Hooks have a great ability to provide extensibility and this is the reason why they exist. You can use hooks whenever you want to customise, extend or modify the existing functionality or even create a new one without touching the core files. Hence, just like the word, “hook” does the act of hooking your code up to the core code of WordPress.

For example, if I want to add a widget that allows users to easily choose social profiles to link themselves, I can write such widget as a plugin. And then, add it in our WordPress without having to worry about losing the changes the very next time we upgrade our WordPress core files. Isn’t it great? This is what makes hook more powerful and loved by developers.

Types of WordPress Hooks

WordPress hooks are of two types: Action Hooks and Filter Hooks. You can execute your own code with the help of action hook whereas, a filter hook lets you change a value and return it in a new form. In simpler words, if you want to do something in WordPress then use action hook and if you want to change something then use filter hook.

Note: You need to write a custom function, called “callback” and then register it in WordPress if you want to use both action and filter hooks.

Now, let’s take a closer look at each one of these WordPress hooks.

Action Hooks

At the very basic level, action hooks are “do something” hooks, which allows you to execute your custom functions. It is a place in the code where you can do something or you can take an action on. Whenever specific events occur, WordPress core launch action hooks to provide you with an extra functionality. An event may be anything, be it publishing a post or, adding instructions above the login form, be it changing a theme or activating a plugin.

If you see “add_action()”  somewhere in the code then, you should be aware that you have come across the action hook. To use an action hook, you need to create a function in the function.php file and then, hook it with add_action() function as follows:

function wp_head() {
do_action(‘wp_head’);
}

“wp-head” is the most commonly used action hook by many plugins and themes to add things such as Meta-data between the head tags of a WordPress page.

Now, let’s jump to another add action example.

<?php

add_action( ‘wp_head’, ‘wpaussie_actionhook_example’ );

function wpaussie_actionhook_example () {

echo ‘<meta name=”description” content=”Meta description for this page”>’ .”

”;

}// End wpaussie_actionhook_example()

?>

The code above adds the text “Hello WPAussie Readers!” between your theme’s <head> tags. If you place “wp_head” in the call to add_action() with “get_header” then, this text would be displayed above your theme.

Filter Hooks

Filter hooks are “customised” hooks, which are used to change a value and return it to a new form. Overall, filter hooks are used in the manipulation of text or other output, be it truncating text or attaching links to related posts, be it changing content formatting or changing an option retrieved from the database. Unlike action hook, filter hook modifies and manages data before it is sent to the browser screen or is saved from the browser to the database.

If you see following codes in your WordPress then, you have come across filter hooks.

  • apply_filters( $tag, $value );
  • apply_filters_ref_array( $tag, $args );

Following is an example of filter hooks being used:

<? php

add_filter( ‘the_content’,  ‘wpaussie_filterhook_signoff’ );

function wpaussie_filterhook_signoff ( $content ) {

if ( is_single() ) {

$content = ‘ <div class =”sign-off”>That’s it, dude!</div>’ . ”

”;

} // End IF Statement

Return $content;

} // End wpaussie_filterhook_signoff()

?>

The code above adds sign-off to the end of each blog post on a single blog post screen.

In one way, filter hooks are a great opportunity to modify a value or data during the execution of your site before it is used to do something. The main purpose of it is to modify settings before going through the output result.

Anatomy of Hooks

As mentioned above, WordPress hooks are of two types and when we use them, we either use add_action() function or add_filter() function.

Now, what you need to know is, both these function take four parameters:

  • Tag

The tag is the first parameter of both functions, which tells WordPress where to hook your function and when should it be executed.

  • Hooked Function

The second parameter of both functions is a hooked function, which should be the exact name of the function. It contains letters, numbers and underscores since it is a name function. It is always recommended to make the function name readable and memorable.

  • Hook Priority

As the name implies, hook priority determines the order in which it is executed when multiple functions are hooked in the same tag. The lower the priority, the sooner the functions get executed.

  • Other Parameters

The last parameter places the number of parameters passed to our hooked function. This parameter is 1 by default but, some tags can have more.

Finally, Get hooked?

I hope this article helped you to get some ideas about hooks and given you some taste to identify them. WordPress hooks is a huge topic itself, if you want to learn it more in detail then, you surely need to put your hands in WordPress and start coding. After all, the best way to learn anything is by doing yourself. As you have already got the basics here, I am pretty sure you can be okay to get started.

Are you ready to get hooked? 😀


Updated on: 17 August 2016 |


Sujata Shrestha

Sujata Shrestha

Meet Sujata Shrestha, a content writer specialising in WordPress SEO. With 7+ years of experience, Sujata crafts engaging and optimised content that drives traffic to clients' websites. Her industry knowledge and passion for staying up-to-date with the latest trends help businesses achieve their online goals.