WordPress Hooks: Actions and Filters

Hooks are the most important idea in WordPress development. A hook is a place in the WordPress code where you can run your own function, or change some data, without editing the core files. Themes and plugins both use hooks. There are two types: actions and filters.

PointActionFilter
PurposeDo something at a momentChange a value and give it back
Add withadd_action()add_filter()
Run bydo_action()apply_filters()
Return valueNot neededMust return the (changed) value

Actions

An action runs your function when WordPress reaches that moment. Here the function is attached to a hook which we create ourselves with do_action():

function tc_say_hello( $name ) {
	echo "Hello, $name!\n";
}
add_action( 'tc_greeting', 'tc_say_hello' );

do_action( 'tc_greeting', 'Riya' );
echo 'Times the action ran: ' . did_action( 'tc_greeting' );
Output

Hello, Riya!
Times the action ran: 1

Filters

A filter receives a value, changes it and returns it. Always return the value, even if you did not change it, otherwise the content will disappear.

function tc_add_signature( $content ) {
	return $content . ' -- Team Tutorials Class';
}
add_filter( 'tc_message', 'tc_add_signature' );

echo apply_filters( 'tc_message', 'Thanks for reading' );
Output

Thanks for reading — Team Tutorials Class

Priority and accepted arguments

add_action( $hook, $callback, $priority = 10, $accepted_args = 1 ). A lower priority number runs earlier. The default is 10.

add_action( 'tc_steps', function () { echo "B (priority 10)\n"; } );
add_action( 'tc_steps', function () { echo "A (priority 5)\n"; }, 5 );
add_action( 'tc_steps', function () { echo "C (priority 20)\n"; }, 20 );
do_action( 'tc_steps' );
Output

A (priority 5)
B (priority 10)
C (priority 20)

If the hook passes more than one value, say how many your function wants as the fourth parameter:

add_filter( 'tc_price', function ( $price, $tax_percent ) {
	return $price + ( $price * $tax_percent / 100 );
}, 10, 2 );

echo apply_filters( 'tc_price', 200, 18 );
Output

236

Remove a hook

function tc_noisy() { echo "noisy\n"; }
add_action( 'tc_event', 'tc_noisy' );
remove_action( 'tc_event', 'tc_noisy' );
do_action( 'tc_event' );
echo has_action( 'tc_event', 'tc_noisy' ) ? 'still attached' : 'removed';
Output

removed

Real WordPress hooks

HookTypeWhen it runs
initactionAfter WordPress is loaded. Register post types and taxonomies here
after_setup_themeactionTheme setup, add_theme_support
wp_enqueue_scriptsactionLoad CSS and JS on the front
wp_head, wp_footeractionInside the head and before the closing body tag
widgets_initactionRegister sidebars and widgets
save_postactionAfter a post is saved
admin_menuactionAdd pages to the dashboard menu
the_contentfilterChange the content of a post before it is shown
the_titlefilterChange post titles
body_classfilterAdd classes to the body tag

A real filter on the_content

Terminal output

$ curl -s http://example.com/hello-world/ | grep "Thanks for reading"
<p><em>Thanks for reading this post.</em></p></article>

Note: Search the WordPress source or the Developer Reference for do_action( and apply_filters( to find the hooks you can use. Next: Shortcode API.

Congratulations! Chapter Finished. Learn more about the similar topics:
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.