Learn how WordPress hooks allow developers to add functionality and modify website behavior without directly editing WordPress core files.
WordPress hooks are points in the WordPress execution process where developers can run their own code or modify certain data. They help developers extend WordPress functionality without changing the original core files.
Hooks are commonly used in themes and plugins to add features, customize content, load scripts, modify titles, and perform other tasks.
WordPress has two main types of hooks:
Action hooks allow you to execute your own code at a particular point in WordPress.
Example:
add_action()
Filter hooks allow you to modify data and return the modified value.
Example:
add_filter()
An action hook allows a function to run when WordPress reaches a particular event or execution point.
For example, the admin_notices action can be used
to display a notice in the WordPress administration area.
<?php
function my_custom_admin_notice() {
echo '<div class="notice notice-success is-dismissible">';
echo '<p>Welcome to WordPress!</p>';
echo '</div>';
}
add_action(
'admin_notices',
'my_custom_admin_notice'
);
Explanation:
my_custom_admin_notice() is our custom function.admin_notices is the action hook.add_action() connects the function to the hook.functions.php file. It is intended for the
administration area, not the public-facing website.
The basic syntax is:
add_action(
$hook_name,
$callback,
$priority,
$accepted_args
);
| Parameter | Meaning |
|---|---|
$hook_name |
The name of the action hook. |
$callback |
The function or callback to execute. |
$priority |
Controls the execution order. The default is 10. |
$accepted_args |
The number of hook arguments accepted by the callback. The default is 1. |
A filter hook lets you receive a value, modify it, and return the updated value. WordPress then uses the filtered value.
<?php
function my_custom_excerpt_length($length) {
return 20;
}
add_filter(
'excerpt_length',
'my_custom_excerpt_length'
);
Explanation:
excerpt_length is the filter hook.$length receives the current excerpt length value.return 20; supplies the modified value.The exact displayed excerpt length can depend on the theme or plugin and how it generates the excerpt.
add_filter(
$hook_name,
$callback,
$priority,
$accepted_args
);
The parameters work similarly to add_action().
The important difference is that a filter callback normally
receives a value and must return the value that WordPress should
continue using.
| Action Hook | Filter Hook |
|---|---|
| Runs code at a particular point. | Modifies a value as it passes through a hook. |
Commonly registered using add_action(). |
Commonly registered using add_filter(). |
| Does not use a return value to modify hook data. | Normally returns the modified value. |
Example: admin_notices. |
Example: excerpt_length. |
| Useful for triggering tasks. | Useful for changing content or settings values. |
Here are some useful action hooks for WordPress developers:
| Hook | Purpose |
|---|---|
init |
Runs after WordPress has loaded but before headers are sent. |
wp_enqueue_scripts |
Loads front-end scripts and styles. |
admin_enqueue_scripts |
Loads scripts and styles in the admin area. |
admin_notices |
Displays notices in the admin area. |
wp_footer |
Runs near the end of the front-end page output. |
save_post |
Runs when a post is saved or updated, subject to its hook behavior and arguments. |
| Hook | Purpose |
|---|---|
the_title |
Filters a post or page title. |
the_content |
Filters post content before it is displayed through the relevant content pipeline. |
the_excerpt |
Filters the post excerpt. |
excerpt_length |
Filters the default excerpt word length. |
body_class |
Filters the list of CSS classes applied to the body element. |
document_title_parts |
Filters the parts used to build the document title. |
The body_class filter allows you to add a custom CSS
class to the classes WordPress generates for the body element.
<?php
function my_custom_body_class($classes) {
$classes[] = 'my-custom-page';
return $classes;
}
add_filter(
'body_class',
'my_custom_body_class'
);
This adds my-custom-page to the body class list
when the theme uses the WordPress body_class()
template tag.
You can then style that class in your CSS:
.my-custom-page {
background-color: #f4f6f9;
}
Hook priority determines the order in which callbacks attached to the same hook run. A lower number runs earlier than a higher number.
add_action(
'init',
'first_custom_function',
5
);
add_action(
'init',
'second_custom_function',
20
);
In this example, first_custom_function()
runs before second_custom_function() when
both callbacks are registered and the init
hook executes.
functions.php
for custom functionality associated with that theme.
Remember that the code stops being loaded when the
child theme is no longer active.
admin_notices action to display a welcome notice.excerpt_length filter to change the default excerpt length.body_class filter to add a custom class.add_action() and add_filter().add_action() registers an action callback.add_filter() registers a filter callback.Choose the correct answer for each question.