If you’re developing with WordPress, hooks are one of the first concepts you need to master. Since modifying WordPress core files is strictly forbidden, hooks are the way to customize behavior. They allow developers to “attach” their own code to WordPress core, themes, and plugins.
In this guide, we’ll explain what hooks are, cover the two types (Actions and Filters), walk through practical examples, and show how to create your own hooks.
Explanation of Terms
Hook: A generic term in WordPress referring to locations where you can add your own code or change the actions performed or output by WordPress. There are two types of hooks: Actions & Filters.
What is an Action in WordPress?
An action in WordPress is a hook that occurs at a specific time when WordPress runs, allowing you to perform a specific action at that time. For example, an action could be creating a custom post type when WordPress is initialized or sharing your post on Facebook when you publish it.
What is a Filter in WordPress?
A filter allows you to modify data before it is sent to the database or the browser. For example, you can add content at the end of each post or change the excerpt length.
Working with hooks starts with identifying which hook to attach your code to, then writing the function that modifies data (filter) or performs an action.
How to attach or remove hook attachments?
Attaching your own functions is straightforward. For actions, you need the hook name and when it should run. For filters, you also need to know what type of value you receive and what to return.
How to attach to an action
add_action( $hook, $function_to_add, $priority, $accepted_args );The required parameters for the add_action function are the hook you want to attach to and the name of the function you want to attach to it.
The priority parameter is optional (default 10) and controls execution order – lower numbers run first. The $accepted_args parameter specifies how many arguments the callback receives.
For your information, the default value for the priority parameter is 10.
How to attach to a filter
add_filter( $tag, $function_to_add, $priority, $accepted_args );The add_filter function works similarly to add_action, but remember that for a filter, the function_to_add parameter should accept a value and return it at the end of the function.
How to Remove Hook Attachments
Use remove_action() or remove_filter() with the hook name, function name, and priority. The priority must match the one used when the function was originally attached.
remove_action( $tag, $function_to_remove, $priority );remove_filter( $tag, $function_to_remove, $priority );After understanding the basics of how to remove and attach functions, let’s look at some real-world examples of the use of hooks.
Examples of Using Hooks – WordPress
WordPress has hundreds of built-in hooks, and plugins and themes add their own. Below are practical examples using WordPress core hooks and the Yoast SEO plugin.
1. Adding a link to the home page in the footer
<?php
/****** BEGIN HERE ******/
function add_footer_link() { ?>
<a href="https://savvy.co.il">Savvy.co.il - WordPress Developer & Designer</a>
<?php
}
add_action( 'wp_footer', 'add_footer_link' );2. Changing the Excerpt Length
function excerpt_length_example( $words ) {
return 15;
}
add_filter( 'excerpt_length', 'excerpt_length_example' );This filter controls the number of words returned by the_excerpt(). We return 15 to limit the excerpt to 15 words.
If you’re unsure what type of value the filter is supposed to receive, search the WordPress core code for
apply_filters( 'filter_name'and take a closer look at what happens with that filter.
3. Attaching to the Publish Post Action
function publish_post_tweet($post_ID) {
global $post;
// Code to send a tweet with post info
}
add_action('publish_post', 'publish_post_tweet');This hooks into the publish_post action, which fires when a post is published. You can use it to trigger any logic at that point, such as sending a notification or sharing on social media.
4. Attaching to the Enqueue Scripts and Styles Action
function themeslug_enqueue_style() {
wp_enqueue_style( 'core', 'style.css', false );
}
function themeslug_enqueue_script() {
wp_enqueue_script( 'my-js', 'filename.js', false );
}
add_action( 'wp_enqueue_scripts', 'themeslug_enqueue_style' );
add_action( 'wp_enqueue_scripts', 'themeslug_enqueue_script' );The wp_enqueue_scripts action is the proper way to add JavaScript and CSS files to your WordPress site. Always use this hook instead of hard-coding script and style tags.
This is the place to mention, if it wasn’t clear – you can attach as many functions as you want to the same hook!
5. Adding the Page Number to the Meta Title Created by Yoast SEO
As mentioned at the beginning of the article, many plugins provide us with hooks to modify their functionality.
function change_yoast_title($title) {
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
if ( is_paged() ) {
return $title . ' - Page ' . $paged;
}
else {
// first page of pagination
return $title;
}
}
add_filter('wpseo_title','change_yoast_title');This adds the page number to the meta title on paginated pages. The wpseo_title filter from Yoast SEO receives the page title and returns a modified version. We check if the page is paginated using is_paged(), and if so, append “Page” and the number.
6. Removing Canonical URLs from your search results pages
An action that SEO experts will likely appreciate. Assuming you are using Yoast SEO, the following filter will remove the Canonical URL from the search results pages of WordPress:
function yoast_remove_canonical_search( $canonical ) {
if( is_search() ) {
return false;
} else {
return $canonical;
}
}
add_filter('wpseo_canonical', 'yoast_remove_canonical_search');Useful Hook Inspection Functions
WordPress provides several functions to inspect and debug hooks at runtime:
has_filter( $hook, $callback )– Check if a specific callback is attached to a hook. Works for both actions and filters.did_action( $hook )– Returns the number of times an action has fired. Useful for preventing duplicate execution.current_action()/current_filter()– Returns the name of the hook currently being executed.remove_all_actions( $hook )/remove_all_filters( $hook )– Removes all callbacks from a specific hook.
Naming convention: When creating custom hooks, always prefix them with your plugin or theme name (e.g. savvy_after_header instead of after_header) to avoid conflicts with other plugins.
Create Your Own Hooks
Creating your own hooks is straightforward. Use do_action() for custom actions and apply_filters() for custom filters.
1. Define Your Hook
Add the following code to your functions.php file (or your plugin):
function custom_hook() {
do_action('custom_hook');
}This registers an action named custom_hook. Use a descriptive name that reflects what the hook does.
2. Add the Hook to Your Template
Add the call to the function wherever you want in your template:
<?php custom_hook(); ?>You can simply add
do_action('custom_hook');directly in the template.
Your template now has a custom hook. Any developer can attach functions to it using add_action().
3. Add the Function Itself
Now, to add your own code at this point, you need to use the following code:
if ( ! function_exists( 'your_function_name' ) ) {
function your_function_name() {
// Your custom code goes here
}
add_action( 'custom_hook', 'your_function_name' );
}Note that we wrapped the function in
function_exists()to prevent errors in case a function with the same name already exists.
4. Creating a Custom Filter
You can also create custom filters using apply_filters():
function get_custom_greeting( $name ) {
$greeting = 'Hello, ' . $name;
return apply_filters( 'custom_greeting', $greeting, $name );
}Other developers can now modify the greeting:
function modify_greeting( $greeting, $name ) {
return 'Welcome, ' . $name . '!';
}
add_filter( 'custom_greeting', 'modify_greeting', 10, 2 );FAQs
Summary
Hooks are the foundation of WordPress extensibility. Actions let you execute code at specific points, while filters let you modify data before it’s used. Understanding these two concepts opens the door to customizing virtually any aspect of WordPress – from simple tweaks to complex WooCommerce customizations.
The key takeaway: always use hooks instead of editing core files, prefix your custom hooks to avoid conflicts, and remember that filters must always return a value.

