fbpx

Use Actions and Filters in Classes in WordPress

Read Time:2 Minute, 33 Second

Developing plugins for WordPress allows you to extend WordPress’ core functionality. In a previous guide, we explained how to create a WordPress plugin. In this guide, we are going to explain how to use actions and filters in classes in WordPress.

Using classes

You can use classes in WordPress the same way you would in any other project. The issue for a lot of people, and beginners especially, is using actions and filters in a class. If you just add the action or filter as you would outside of a class, it won’t work because of the way WordPress works with classes.

Let’s say we have the following class, and you want to display the “Hello!” text in the footer.

class MyCoolestClass {
    
    private $helloText = 'Hello!';
    
    public function __construct()
    {
        
    }

}

Use Actions and Filters in Classes – The Wrong Way

The way we would do this outside of a class would be as we usually use actions, like in the following example.

function kodacoding_add_some_footer_text() {
    echo "Hello!";
}
add_action( 'wp_footer', 'kodacoding_add_some_footer_text' );

But we want to use this action inside a class and if you will try to use the above code in a class, it will not work.

Use Actions and Filters in Classes – The Right Way

To use an action or filter in a class, we need to let the “add_action” function know not only what function to execute but also from what class, which will usually be the same class you are working on.

class MyCoolestClass {

    private $helloText = 'Hello!';

    public function __construct()
    {
        add_action( 'wp_footer', array( $this, 'kodacoding_add_some_footer_text' ) );
    }

    public function kodacoding_add_some_footer_text() {
        echo $this->helloText;
    }

}

Then, we will need to initialize a new instance of this class, and the complete code will like the following.

<?php
/**
 * Plugin Name
 *
 * @package           MyCoolestWordpressPlugin
 * @author            KodaCoding
 * @copyright         2022 KodaCoding
 * @license           GPL-2.0-or-later
 *
 * @wordpress-plugin
 * Plugin Name:       My Coolest WordPress Plugin
 * Plugin URI:        https://kodacoding.com/create-a-wordpress-plugin
 * Description:       Create a WordPress plugin tutorial on KodaCoding.com
 * Version:           1.0.0
 * Requires at least: 5.2
 * Requires PHP:      7.2
 * Author:            KodaCoding
 * Author URI:        https://kodacoding.com
 * Text Domain:       my-coolest-wordpress-plugin
 * License:           GPL v2 or later
 * License URI:       http://www.gnu.org/licenses/gpl-2.0.txt
 * Update URI:        https://kodacoding.com/create-a-wordpress-plugin
 */

class MyCoolestClass {

    private $helloText = 'Hello!';

    public function __construct()
    {
        add_action( 'wp_footer', array( $this, 'kodacoding_add_some_footer_text' ) );
    }

    public function kodacoding_add_some_footer_text() {
        echo $this->helloText;
    }

}

$myCoolestClass = new MyCoolestClass();

Of course, this is a simple example of how to use WordPress actions in a class. You can make your class as complicated as you need it to be. Do notice though your code is clear, readable, and easy as possible to maintain.
If you need, you can of course add more than one class to a single plugin, just try to keep it as organized as possible.
To use filters, replace the “add_action” function with the “add_filter” function.

Leave a Reply

Your email address will not be published. Required fields are marked *