What are WordPress Hooks?

Website owners have been modifying publishing platforms as long as the internet has been around. When I first started working online, modifying a script such as vBulletin or PHPNuke was a timely affair. This was a time before plugins existed.

To add a new feature to your website, you had to spend time modifying core files directly (this was sometimes referred to as hacking as you had to hack the code). This usually meant making several changes to several different files. It was time consuming as you had to make sure that you added code correctly. Errors were common.

Problems arose when it was time to upgrade as updating the core files would overwrite all the changes you had applied; which meant that you had to apply all modifications again.

Can you imagine having to spend four hours applying updates to your website every time WordPress was updated? Well, ten to fifteen years ago, that is what many website owners like me did.

Thankfully, you do not need to do that today because of plugins. WordPress plugins allow you to add functions to your website without changing the core files. It is a much more efficient way of changing an existing application.

What are WordPress Hooks?

Plugins can change the way that WordPress works by using hooks. They do this by inserting code into WordPress in specific locations. Or if you prefer, plugins hook into the WordPress core.

There are two ways in which plugins can hook into the WordPress core:

  • Actions
  • Filters

Action hooks are activated when a specific event occurs, such as publishing a blog post, loading the footer, or editing an attachment. Your function would therefore be run when the action occurs.

Filter hooks are used to modify output. WordPress can pass data through a filter so that the output is modified in some way or another. For example, it could be used to change the way an article is displayed or cut the length of a blog post title.

There are a lot of hooks to choose from. Adam Brown’s WordPress Hooks Database currently lists 1,616 available WordPress hooks; and a few more are added with each version of WordPress.

You have probably came across hooks in the past and not realised it as hooks are used in every single WordPress plugin and theme.

A hook looks like this:

add_action ( 'hook_name', 'your_function_name', [priority], [accepted_args] );

The hook name is provided by WordPress. You enter the name of your own function in the second argument. The last two arguments are optional. Priority changes the order in which functions are executed and accepted_args defines how many arguments your function can accept.

One of the most commonly used action hooks is wp_head. The hook allows you to insert a function directly into the <head> area of WordPress.

You can do this by using this code:

add_action('wp_head', 'your_function');

The wp_head hook is used frequently since the <head> area of a web page is used for page titles, meta information, Javascript, calls to stylesheets, and more.

A good example of this can be found on WordPress.org. They show how a plugin can use the wp_head action hook to insert custom styling into a webpage.

add_action('wp_head','hook_css');
function hook_css()
{

$output=" .wp_head_example { background-color : #f1f1f1; } ";

echo $output;

}

Filter hooks are very common too. Look at the code below to see how the default WordPress theme Twenty Fourteen changes page titles.

As you can see, the function to modify the the page title is first defined. Next, the function is used to modify the page title by using the wp_title filter hook.

/**
* Create a nicely formatted and more specific title element text for output
* in head of document, based on current view.
*
* @since Twenty Fourteen 1.0
*
* @param string $title Default title text for current view.
* @param string $sep Optional separator.
* @return string The filtered title.
*/
function twentyfourteen_wp_title( $title, $sep ) {
global $paged, $page;

if ( is_feed() ) {
return $title;
}

// Add the site name.
$title .= get_bloginfo( 'name' );

// Add the site description for the home/front page.
$site_description = get_bloginfo( 'description', 'display' );
if ( $site_description &amp;&amp; ( is_home() || is_front_page() ) ) {
$title = "$title $sep $site_description";
}

// Add a page number if necessary.
if ( $paged &gt;= 2 || $page &gt;= 2 ) {
$title = "$title $sep " . sprintf( __( 'Page %s', 'twentyfourteen' ), max( $paged, $page ) );
}

return $title;
}
add_filter( 'wp_title', 'twentyfourteen_wp_title', 10, 2 );

I have only touched upon what can be done with WordPress hooks, though I hope you have a better understanding of what they are and what they can do.

Essentially, hooks are the foundation of the WordPress plugin system. With over 1,600 hooks available, you can use action hooks and filter hooks to manipulate your website in many weird and wonderful ways.

For more information about hooks, please refer to the following pages:

Good luck,
Kevin

About the author

Kevin Muldoon is a professional blogger with a love of travel. He writes regularly about internet marketing on his personal blog and on the internet marketing discussion forum Rise Forums.

2 Responses to What are WordPress Hooks?

  • @MarkGavalda

    Code formatting would help this article a lot, Kevin :)

  • Kevin Muldoon

    I agree Mark. I will speak to Shilpa about this as the current design does not style code tags. Nor are there any syntax plugins installed. :)

Leave a Reply

Your email address will not be published. Required fields are marked *

Send me notification of future comments via e-mail

Get Over 88 Premium WordPress Products in WPEka Club - Use Coupon Code WPEKA35BAR Sign Up Now
To go for WPEka Club Price Increase Join Now to LOCK in at these low prices