Actions do stuff, filters change stuff

Actions do stuff, filters change stuff

This post is going to be a bit different – it will summarize actions and filters that exist within WordPress ecosystem. There are numerous articles describing action and filter hooks and all that information might appear overwhelming. From all the talk and information about hooks, I find these definitions to explain it in the best way:

Actions allow you to add extra functionality at a specific point in the processing of the page.
Filters allow you to intercept and modify data as it is processed.

Actions
You’ll find two functions when it comes to action hooks (there are more than two, though, such as remove_action(), but we are not interested in them at this point):

do_action
add_action
The first question would normally be – when to use either of these? It may be a very rough answer, but I’ll give it a shot:

If you’re developing a theme or a plugin, there is a chance that you will use both.
If you’re making some customizations on a theme (adding some functionality) or a plugin, it is likely that you will need add_action only.
Let’s have a look at wp_footer() function, which also exists as an action hook.

function wp_footer() {
    /**
     * Print scripts or data before the closing body tag on the front end.
     *
     * @since 1.5.1
     */
    do_action( 'wp_footer' );
}

This is copied directly from WordPress core. If we want to output something (such as function) into wp_footer() action hook, we would use add_action:

function output_some_text() {
echo 'This outputs some text into footer';
}
add_action( 'wp_footer', 'output_some_text' );

At this point, the logical question is – how do I know if there is an action hook (that I would need at a certain point) available for the theme, plugin or WordPress? You will need to dig into documentation – for WordPress, you can find all the action hooks inside official code reference. Advanced plugins such as WooCommerce also have a generous list of hooks – take a peek.

To conclude – action hooks don’t change what’s already there, they are used for adding something new instead.

Filters
Filter is another type of hook and comes in two flavors (as with action hooks, there are some other filters available, but we are not interested in them at this point):

apply_filters
add_filter
The difference is the same as it is the case with action hooks – if you are developing something from scratch, you will likely use apply_filters. On the other hand, if you are doing some changes on the existing themes are plugins, you will use add_filter. As an example, let’s create a function that will not earn us the best developer award – it will only disable the logout link in the WordPress admin area. Let’s have a look at the wp_logout_url() function and logout_url filter (copied directly from WordPress core):

function wp_logout_url($redirect = '') {
    $args = array( 'action' => 'logout' );
    if ( !empty($redirect) ) {
        $args['redirect_to'] = urlencode( $redirect );
    }
 
    $logout_url = add_query_arg($args, site_url('wp-login.php', 'login'));
    $logout_url = wp_nonce_url( $logout_url, 'log-out' );
 
    /**
     * Filter the logout URL.
     *
     * @since 2.8.0
     *
     * @param string $logout_url The Log Out URL.
     * @param string $redirect   Path to redirect to on logout.
     */
    return apply_filters( 'logout_url', $logout_url, $redirect );
}

For this purpose, we will want to change the $logout_url variable only, so we will use add_filter() in the following way:

function disable_logout_link ($logout_url) {
   $logout_url = ''; //no link defined
   return $logout_url;  
   
}
add_filter( 'logout_url', 'disable_logout_link' )

The possibilities of filters are endless, since filter hooks are frequent in many plugins, themes and most importantly – WordPress core has numerous filters available, allowing us to change practically every single part of it.