WordPress plugins and themes interact with core code using WordPress hooks. There are two different types of hooks.
- Actions: These are used to add or change WordPress functionality.
- Filters: These are used to alter the functionality of actions.
If you’re going to start coding your own plugins, You should familiarize yourself with how actions and filters work and which ones are available for you to use.
1. Actions and Action Hooks
In visiting any page of a WordPress website, a series of PHP functions (named actions) are called at various points, and they are attached to action hooks. Using the action hooks provided by WordPress, you can add your own functions to the list of actions that run when any action hook is calling, and you can also remove pre-existing functions from any action hook.
1.1. Adding Functions To An Action Hook Using add_action()
To add a function to any action hook, your plugin must call the WordPress function named add_action(), with at least two parameters.
function YOUR_FUNCTION_NAME(){
//Function code goes here
}
add_action( 'ACTION_HOOK', 'YOUR_FUNCTION_NAME' , PRIORITY, NUMBER_OF_ARGS);
- The first required parameter is the name of the action hook that you want to attach to
- The second required parameter is the name of the function that you want to run
- The third parameter (optional) is the priority of the function you want to run.
- The fourth parameter (optional) is the number of arguments, which means how many parameters your custom function is able to take. The default is 1.
For example:
function hs_add_Cookie() {
setcookie("hs_last_visit_time", date("r"), time()+60*60*24*30, "/");
}
add_action( 'init', 'hs_add_Cookie' );
Hook to the “init” action, which is called after WordPress is finished loading the core code.
add_action("wp_footer", "hs_add_custom_text_to_footer");
function hs_add_custom_text_to_footer()
{
echo "<p style='color: black;'>After the footer is loaded, my text is added!</p>";
}
This plugin hooks the wp_footer() action hook, which is called right before the closing </body> tag of every page, and adds a new function named hs_add_custom_text_to_footer().
1.2. Removing Functions From an Action Hook Using remove_action()
To remove an action from an action hook, you must write a new function that calls remove_action(), then call the function you have written using add_action().
add_action( 'init', 'remove_hs_add_custom_text_to_footer' );
function remove_hs_add_custom_text_to_footer()
{
remove_action('wp_footer', 'hs_add_custom_text_to_footer');
}
In this example we removed hs_add_custom_text_to_footer() function we just add in the Adding Functions To An Action Hook above.
- The first required parameter is the name of the action hook the function is hooked to
- The second required parameter is the name of the function that you want to remove
- The third parameter (optional) is the priority of the original function.
2. Filters and Filter Hooks
A filter function allows you to modify the resulting data that is returned by existing functions and must be hooked to one of the filter hooks. The available filter hooks are different from the action hooks.
They behave similarly to action hooks in that they are called at various points in the script and are contextual. A full list of filter hooks and the context in which they are called can be found on the WordPress Plugin API/Filter Reference page.
1.1. Adding Filters Using add_filter()
To add a filter function to any filter hook, your plugin must call the WordPress function named add_filter(), with at least two parameters.
add_filter('FILTER_HOOK', 'YOUR_FUNCTION_NAME' , PRIORITY, NUMBER_OF_ARGS);
function YOUR_FUNCTION_NAME(ARGUMENTS)
{
//Your code goes here
}
- The first required parameter is the name of the filter hook that you want to hook
- The second required parameter is the name of the filter function that you want to run
- The third parameter (optional) is the priority of the function you want to run.
- The fourth parameter (optional) is the number of arguments, which means how many parameters your custom filter function is able to take. The default is 1.
WordPress has a function that retrieves the content of a post named the_content(). This function is a filter hook. The function to actually display the content calls the_content() to retrieve it, that’s where the filter is applied and the content is altered before being displayed.
This plugin defines a filter function that takes the content as its only input parameter and fixes text spacing every time the the_content() is called.
add_filter("the_content", "hs_fix_text_spacing");
function hs_fix_text_spacing($content)
{
$the_new_content = str_replace(" ", " ", $content);
return $the_new_content;
}
As the return value of the the_content() function is the actual content text, it is automatically entered as the function’s parameter $content when called using add_filter(). The function you define must return the new value.
1.2. Removing Filters Using remove_filter()
Removing a filter is much simpler than removing an action because you can call the remove_filter() function without defining a new function.
remove_filter("the_content", "hs_fix_text_spacing");
In this example, we removed the hs_fix_text_spacing() function we just add in the previous section.