Hooks, Actions & Filters
The WordPress Hook System is how plugins and themes extend WordPress functionality without modifying core files. Hooks are predefined points where you can inject your own code. Actions let you DO something; Filters let you CHANGE something. Master hooks and you master WordPress development.
Prerequisites
- Basic PHP knowledge (functions, parameters)
- Access to functions.php or a custom plugin
- Understanding of WordPress execution flow
Step-by-Step Guide
Understand the Two Types of Hooks
Actions: Goal is to DO something at a specific point in time
Actions add functionality, echo output, register resources
Examples: send email after post published, add menu item, load scripts
Filters: Goal is to CHANGE something before it is used
Filters modify text, variables, arrays, or objects and return them
Examples: modify post content, change excerpt length, filter titles
Master Action Hooks
add_action() attaches your function to a WordPress action hook
Syntax: add_action( $hook_name, $callback_function, $priority, $accepted_args )
Priority (default 10): Lower numbers run earlier
Common hooks: init, wp_enqueue_scripts, admin_menu, wp_footer
// Example: Add custom JavaScript to the front-end
add_action( 'wp_enqueue_scripts', 'my_custom_scripts' );
function my_custom_scripts() {
wp_enqueue_script(
'my-script',
get_stylesheet_directory_uri() . '/js/custom.js',
array( 'jquery' ),
'1.0',
true // Load in footer
);
}
// Example: Run code when WordPress initializes
add_action( 'init', 'my_init_function' );
function my_init_function() {
// Register custom post types here
// This runs after WordPress loads but before headers are sent
}Master Filter Hooks
add_filter() attaches your function to modify data
Your function receives data, modifies it, and MUST return it
Syntax: add_filter( $hook_name, $callback_function, $priority, $accepted_args )
Common filters: the_content, excerpt_length, document_title_parts
// Example: Add content after every post
add_filter( 'the_content', 'add_cta_after_content' );
function add_cta_after_content( $content ) {
if ( is_single() && is_main_query() ) {
$cta = '<div class="post-cta">Thanks for reading! Subscribe for more.</div>';
$content .= $cta;
}
return $content; // MUST return the modified content
}
// Example: Change excerpt length
add_filter( 'excerpt_length', 'custom_excerpt_length' );
function custom_excerpt_length( $length ) {
return 30; // Return 30 words instead of default 55
}
// Example: Modify login page logo URL
add_filter( 'login_headerurl', 'custom_login_url' );
function custom_login_url() {
return home_url(); // Link to homepage instead of wordpress.org
}Understand Priority and Accepted Arguments
Priority (default 10): Controls execution order
Lower priority = runs earlier. Higher = runs later
Use priority 5 to run before defaults, 20+ to run after
Accepted Args (default 1): How many parameters your function receives
Some hooks pass multiple values - check documentation
// Example: Priority in action
add_action( 'wp_head', 'my_early_function', 5 ); // Runs early
add_action( 'wp_head', 'my_late_function', 20 ); // Runs late
// Example: Multiple arguments in filter
add_filter( 'the_title', 'modify_title', 10, 2 );
function modify_title( $title, $post_id ) {
// $title is first argument, $post_id is second
if ( $post_id === 42 ) {
$title = 'Special: ' . $title;
}
return $title;
}Know the Most Common Hooks
init: WordPress loaded, ready for custom post types, taxonomies
wp_enqueue_scripts: Properly load CSS and JavaScript on front-end
admin_menu: Add items to WordPress admin menu
wp_head: Output content in <head> section
wp_footer: Output content before </body>
the_content: Modify post/page content before display
excerpt_length: Change auto-generated excerpt word count
pre_get_posts: Modify main query before database call
Verification Checklist
- Understand difference between Actions (do) and Filters (change)
- Can use add_action() to run code at specific WordPress events
- Can use add_filter() to modify data and always return it
- Understand priority: lower numbers run first
- Know common hooks: init, wp_enqueue_scripts, the_content
Pro Tips
- Search the WordPress codebase for do_action() and apply_filters() to find hooks
- Use remove_action() and remove_filter() to unhook existing functions
- Create your own hooks with do_action() and apply_filters() in plugins
- Anonymous functions work but named functions are easier to remove/debug
- The WordPress Plugin API documentation lists all available hooks