Beginner's Guide for WordPress / Start your WordPress Blog in minutes

What is: Hooks

In WordPress development, hooks are functions that can be applied to an action or filter in WordPress. They are one of the big features that make WordPress so customizable.

Hooks allow developers to change or extend WordPress’ functionality without needing to edit the WordPress core code itself. They do this by running actions and filters, which are PHP functions that perform tasks and make changes to data.

They are used extensively by plugin and theme developers. However, if you’re not a developer, then you can paste code snippets from the web to add new features to your website. Many of these include hooks.

What Are Hooks in WordPress?

What Is a Hook?

Hooks are the foundation of WordPress plugin and theme development. They are places where developers can ‘hook’ their custom code into WordPress at specific locations and change how WordPress operates without editing core files.

Developers use hooks to change or extend the functionality of WordPress. You can also use actions to customize your theme by adding code snippets from online tutorials.

Note: If you’re a beginner, then we strongly caution against editing any WordPress files. Only experienced users who feel comfortable with editing the functions.php file and have some knowledge of PHP should try this.

Beginners should either use a plugin to accomplish the task they want to perform or consult professionals to edit code the code for them.

Before editing any code on your WordPress site we recommend that you backup your website in the event of a coding error. If you don’t have a backup plugin, then be sure to read our article where we compare the best WordPress backup plugins.

There are two types of hooks: filters and actions.

What Is a Filter Hook?

A filter will modify the default behavior of a specific function. It does this by manipulating the data it receives and returning that data to WordPress before it is displayed in the browser.

For example, filters can be used to truncate text, change the formatting of content, attach links to posts, modify blocks on a page, and change options retrieved from the database.

Here’s an example of a hook used with a filter in WordPress:

function wpb_custom_excerpt( $output ) {
  if ( has_excerpt() && ! is_attachment() ) {
    $output .= wpb_continue_reading_link();
  }
  return $output;
}
add_filter( 'get_the_excerpt', 'wpb_custom_excerpt' );

The sample code above creates a function wpb_custom_excerpt which is hooked into get_the_excerpt filter.

What Is an Action Hook?

An action will modify the default behavior of a specific function. It does this by taking some information from WordPress and then doing something with it. Once the action has been performed, there is no need to pass any information back to WordPress.

For example, actions can be used to place a promotional message on a page, activate a plugin, add extra widgets to a sidebar, publish a post, or add a menu to a header.

Here’s an example of a hook applied to an action in WordPress:

function mytheme_enqueue_script() {
    wp_enqueue_script( 'my-custom-js', 'custom.js', false );
}
add_action( 'wp_enqueue_scripts', 'mytheme_enqueue_script' );

The sample code above creates a function mytheme_enqueue_script which is hooked into wp_enqueue_scripts action.

We hope this article helped you learn more about hooks in WordPress. You may also want to see our Additional Reading list below for related articles on useful WordPress tips, tricks, and ideas.

If you liked this guide, then please consider subscribing to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

Additional Reading

The Ultimate WordPress Toolkit

Get FREE access to our toolkit - a collection of WordPress related products and resources that every professional should have!