Using hooks
Where to add custom code
If you use a hook to add or manipulate code, you can add your custom code in a variety of ways:
- To a custom child theme’s
functions.php
file. - Using a plugin such as Code Snippets.
Using action hooks
To execute your own code, you hook in by using the action hook do_action('action_name');
. It will look something like so:
add_action( 'action_name', 'your_function_name' ); function your_function_name() { // Your code }
Actions are a standard feature of WordPress. To find out more please check the official WordPress documentation.
Using filter hooks
Filters are used to manipulate a value. Filter hooks are called throughout are code using apply_filter( 'filter_name', $variable );
. To manipulate the passed variable, you can do something like the following:
add_filter( 'filter_name', 'your_function_name' ); function your_function_name( $variable ) { // Your code return $variable; }
With filters, you must return a value.
Filters are a standard feature of WordPress. To find out more please check the official WordPress documentation.