Alternative Ways of Triggering Events in WordPress

Share this article

Alternative Ways of Triggering Events in WordPress

In the first part of this series on the WordPress hook system, we learned about the WordPress hook system and the two types of hooks actions and filters alongside some code examples of how they work.

In this second part of this series, we’ll be learning about alternative ways of triggering events in WordPress and how to hook static and non-static class methods to actions and filters.

WordPress Hooks

In the previous article, I made the following statement:

At various stages of WordPress execution, a large number of events are triggered commonly using the do_actions() and apply_filters() PHP functions. These events can be subscribed or hooked to via add_action() and add_filter().

Take note of my use of the word “commonly”. There are other ways events can be triggered. We’ll explore that in the second part of this tutorial.

The other ways events can be triggered are via do_action_ref_array() function for action hooks and apply_filters_ref_array() for filter hooks.

Both do_action(), do_action_ref_array() and apply_filters(), apply_filters_ref_array() are the same in that each pair are used to execute functions hooked to specific action and filter respectively. The difference is in how they specify their argument.

Unlike do_action() and apply_filters(), do_action_ref_array() and apply_filters_ref_array() specifies their argument as an array.

Let’s see some code examples to better understand how they work.

Code Examples

The action user_profile_update_errors is fired in WordPress before user profile update errors are returned and the profile is updated.

Say you added a custom field to the WordPress user profile and wanted to validate its input before WordPress saves the data to the database. This is the hook you need.

Here is how it is defined in WordPress core.

    /**
     * Fires before user profile update errors are returned.
     *
     * @since 2.8.0
     *
     * @param WP_Error &$errors WP_Error object, passed by reference.
     * @param bool     $update  Whether this is a user update.
     * @param WP_User  &$user   WP_User object, passed by reference.
     */
    do_action_ref_array( 'user_profile_update_errors', array( &$errors, $update, &$user ) );

The code below ensures a custom profile field named city (where users can enter their cities) is not left empty. If it is, an error to that effect will be displayed.

add_action( 'user_profile_update_errors', function ( $errors, $update, $user ) {
        if ( empty( $_POST['city'] ) ) {
            $errors->add( 'city_empty', __( 'City field cannot be left empty.' ) );
        }
    }, 10, 3 );

Let’s see a code example of apply_filters_ref_array().

The code below hooks into the bp_activity_permalink_redirect_url filter in bbPress to modify the intended redirect URL to http://website.com/custom-page/ before the redirect occurs for a single activity item.

add_filter( 'bp_activity_permalink_redirect_url', function ( $redirect, $activity ) {
        $redirect = 'http://website.com/custom-page/';

        return $redirect;

    }, 10, 2 );

When to Use do_action_ref_array() and apply_filters_ref_array()

In making your plugin or theme extensible by other developers, do_action_ref_array() and apply_filters_ref_array() are preferable to do_action() and apply_filters() when there are many additional variables or values to be passed to functions that hook into an action and filter are many.

Take for instance, you’re developing a user registration plugin, and you defined an action to fire after registration is complete with the registered user’s username, email address, first name, last name, address, city, state and country available to functions that hook to it. Here is how the code might look when you use do_action()

do_action('after_user_registration_completed', $username, $email, $firstname, $lastname, $address, $city, $state, $country);

Notice how the list of arguments makes the line of code long and ugly. Now compare the above with that of do_action_ref_array() below.

do_action_ref_array(
    'after_user_registration_completed',
    array(
        $username,
        $email,
        $firstname,
        $lastname,
        $address,
        $city,
        $state,
        $country
    )
);

Hooking Class Methods to Actions and Filters

The code examples we’ve been examining are about hooking named and anonymous functions to action and filter hooks.

So let’s see how to call or include hooks via add_action() and add_filter() within a class for processing during WordPress execution and also how class methods (static and non-static) can be hooked to actions and filters.

Most WordPress developers include all add_action() and add_filter() function calls in their class constructor which is then executed on instantiation like so:

class DemoPlugin {
    public function __construct() {
        add_action( 'wp_head', array( $this, 'google_site_verification' ) );

        add_filter( 'the_content', array( $this, 'we_love_sitepoint' ) );
    }


    /**
     * Include Google site verification meta tag to WordPress header.
     */
    public function google_site_verification() {
        echo '<meta name="google-site-verification" content="ytl89rlFsAzH7dWLs_U2mdlivbrr_jgV4Gq7wClHDUJ8" />';
    }


    /**
     * Append and prepend the text "We love SitePoint" to every post content.
     *
     * @param string $content
     *
     * @return string
     */
    public function we_love_sitepoint( $content ) {
        $text    = sprintf( '<div class="notice alert">%s</div>', __( 'We love SitePoint', 'sp' ) );
        $content = $text . $content . $text;

        return $content;
    }
}


new DemoPlugin();

From the code snippet above, you will discover that there is a difference in the way a function and class method is hooked to an action or filter in that, for a class method, the second argument of add_action() and add_filter() is an array of $this (a reference to the current object) and the method name.

For a static method, the class name is used instead of $this.

class DemoPlugin {
    public function __construct() {
        add_action( 'wp_head', array( 'DemoPlugin', 'google_site_verification' ) );
    }


    /**
     * Include Google site verification meta tag to WordPress header.
     */
    public static function google_site_verification() {
        echo '<meta name="google-site-verification" content="ytl89rlFsAzH7dWLs_U2mdlivbrr_jgV4Gq7wClHDUJ8" />';
    }
}

new DemoPlugin();

The above approach of including the class name for every static method you want to hook to a filter or action violates the don’t repeat yourself (DRY) principle and thus will make refactoring hard.

Instead, use the constant __CLASS__ which returns the class name it was declared in.

class DemoPlugin {
    public function __construct() {
        add_action( 'wp_head', array( __CLASS__, 'google_site_verification' ) );
    }

    // ...
}

new DemoPlugin();

Although I strongly discourage this, here is another way of including a static method to a hook.

class DemoPlugin {
    public function __construct() {
        add_action( 'wp_head', 'DemoPlugin::google_site_verification' );
    }

    // ...
}

new DemoPlugin();

Rather than include all add_action() and add_filter() function calls in a class constructor, I have seen some developers create a static class method which when called, initializes/executes the static methods hooked to an action or filter.

class DemoPlugin {
    public static function init() {
        add_action( 'wp_head', array( __CLASS__, 'google_site_verification' ) );
        add_filter( 'the_content', array( __CLASS__, 'we_love_sitepoint' ) );
    }

    // ...
}


DemoPlugin::init();

In this approach, all methods to be hooked to actions and filters must be static because $this is not accessible in static context.

Summary

In this second part of our series on WordPress hooks, we learned an alternative way of triggering action and filter events, when to use them, and finally, how to hook static and non-static class methods to actions and filters.

In the concluding part, we’ll examine how to hook methods of an instantiated class (object) to an action and filter, how to integrate a namespaced class method to a hook and the caveats of using namespaces in the WordPress hook system.

Happy coding!

Frequently Asked Questions about Triggering Events in WordPress

What are some alternatives to WordPress for triggering events?

While WordPress is a popular platform for triggering events, there are several alternatives that offer similar functionalities. These include Joomla, Drupal, and Squarespace. Joomla is a free and open-source content management system that offers extensive customization options. Drupal is another open-source platform known for its robustness and security. Squarespace, on the other hand, is a paid service that is known for its user-friendly interface and beautiful design templates.

How does triggering events in WordPress compare to other platforms?

WordPress offers a more user-friendly interface compared to other platforms. It has a wide range of plugins that make it easier to trigger events. However, platforms like Joomla and Drupal offer more advanced functionalities and customization options. Squarespace is known for its simplicity and ease of use, but it may not offer the same level of customization as WordPress.

Can I trigger events in WordPress without coding?

Yes, you can trigger events in WordPress without coding. WordPress offers several plugins that allow you to trigger events with just a few clicks. These plugins come with user-friendly interfaces that make it easy to set up and trigger events.

What are some popular plugins for triggering events in WordPress?

Some popular plugins for triggering events in WordPress include Event Espresso, The Events Calendar, and Event Organiser. These plugins offer a range of features including event registration, ticketing, and calendar management.

How secure is WordPress for triggering events?

WordPress is a secure platform for triggering events. It offers several security features including regular updates, strong password enforcement, and two-factor authentication. However, it’s important to keep your WordPress installation and plugins up-to-date to ensure maximum security.

How can I customize the appearance of my events in WordPress?

WordPress offers several ways to customize the appearance of your events. You can use themes and plugins to change the look and feel of your events. You can also use CSS to further customize the appearance of your events.

Can I integrate my WordPress events with other platforms?

Yes, you can integrate your WordPress events with other platforms. WordPress offers several plugins that allow you to integrate your events with platforms like Facebook, Google Calendar, and Eventbrite.

How can I manage my events in WordPress?

WordPress offers several tools for managing your events. You can use plugins to create event calendars, manage registrations, and sell tickets. You can also use the WordPress dashboard to manage your events.

Can I use WordPress to trigger events on a mobile device?

Yes, you can use WordPress to trigger events on a mobile device. WordPress is a responsive platform, which means it automatically adjusts to fit the screen size of the device it’s being viewed on. This makes it easy to trigger events on both desktop and mobile devices.

How can I troubleshoot issues with triggering events in WordPress?

If you’re having trouble triggering events in WordPress, there are several steps you can take. First, make sure your WordPress installation and plugins are up-to-date. If you’re still having trouble, try deactivating and reactivating your plugins to see if that resolves the issue. If all else fails, you may want to reach out to the WordPress community or hire a professional for help.

Collins AgbonghamaCollins Agbonghama
View Author

Collins is a web developer and freelance writer. Creator of the popular ProfilePress and MailOptin WordPress plugins. When not wrangling with code, you can find him writing at his personal blog or on Twitter.

actionsChrisBfiltershooksplugin developmentWordPress
Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week
Loading form