Many tutorials that explain how to write a WordPress plugin (including ours) show the use of global functions for implementing action and filter hooks. In our case we used global functions so we had less to explain. But now that we've gotten past that first tutorial we think once you understand using classes you'll find no reason to use global functions for filter and action hooks ever again.

Converting from Global Functions to Classes

Revisiting our Basic WordPress Plugin example we have:

<?php /* * Plugin Name: Thanks for Reading */ add_filter ( 'the_content' , 'tfr_the_content' ) ; function tfr_the_content ( $content ) { return $content . '<p>Thanks for Reading!</p>' ; } <?php /* * Plugin Name: Thanks for Reading */ add_filter( 'the_content', 'tfr_the_content' ); function tfr_the_content( $content ) { return $content . '<p>Thanks for Reading!</p>'; }

We can easily modify that plugin so it instead uses a class as a code wrapper:

<?php /* * Plugin Name: Thanks for Reading */ class Thanks_For_Reading_Plugin { function __construct ( ) { add_filter ( 'the_content' , array ( $this , 'the_content' ) ) ; } function the_content ( $content ) { return $content . '<p>Thanks for Reading!</p>' ; } } new Thanks_For_Reading_Plugin ( ) ; <?php /* * Plugin Name: Thanks for Reading */ class Thanks_For_Reading_Plugin { function __construct() { add_filter( 'the_content', array( $this, 'the_content' ) ); } function the_content( $content ) { return $content . '<p>Thanks for Reading!</p>'; } } new Thanks_For_Reading_Plugin();

Naming Conventions

Note we changed the name of the function back to the simpler named the_content() because now we can. And we named our class Thanks_For_Reading_Plugin because it's concise, it clearly identifies the plugin, and its highly unlikely to conflict with any other plugin. And we can't say the same for our 'tfr_' prefixed function.

Note that for classes using in WordPress plugins and themes we recommend following the WordPress Coding Standards; Capitalized_Words_Separated_By_Underscores .

Leveraging Features of PHP Classes

The other aspects to discuss are:

So what's a "method?!?" The PHP object constructor named __construct() , The singleton object instantiation using the PHP new operator, The array() wrapper around the method name with $this as first element, and

What is a "Method?"

"Methods" are functions that are specific to classes. Many programming languages implemented object orientation before PHP and collectively they established the "method" terminology. When PHP implemented classes they chose to use the syntax function instead likely because PHP developers were already familiar with the term function .

So it's quite simple really: Methods are functions that are specific to an object or class. If it's a function defined within a class it's called a "method." If it's defined outside of a class it's called a "function."

Did you need to know this? Not really, but it might help you clear up confusion now or in the future.

Use new to Trigger the PHP Object Constructor

The function named __construct() is a special function in PHP designed to automatically run whenever a new object of a class is created. The following code creates a new object and our main purpose for creating the instance is so that the __construct() will be called:

new Thanks_For_Reading_Plugin ( ) ; new Thanks_For_Reading_Plugin();

You don't need to assign the newly created instance to a variable as the code in the constructor that called add_filter() will do that indirectly.

Inside that __construct() method we only need to add a filter hook for 'the_content' :

function __construct ( ) { add_filter ( 'the_content' , array ( $this , 'the_content' ) ) ; } function __construct() { add_filter( 'the_content', array( $this, 'the_content' ) ); }

Using $this with add_filter()

In our basic plugin example we just passed the function name 'tfr_the_content' to our add_filter() call but now that we are using a class we need to specify which object instance will provide the context for our classes' function a.k.a. it's method. The special variable $this acts as the current instance of the class for any code within the class. And when PHP needs a "callable" it can accept a two-element array with the class instance in the first element and the name of the hook in the second element.

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

And that's really all there is to it.

Summary

Switching between function syntax and and class as code-wrapper syntax is effectively trivial yet it allows cleaner code and the ability to protect the names of your functions from conflicting with those of written by another plugin or theme developer. And even if you don't follow understand it all yet, just copy and paste the example and before you know it you'll be surprise that it every seemed confusing.

So now you know, and there's no turning back. 🙂

Follow up Posts

If you'd like to learn more about writing class-based WordPress plugins you might be interested in this post too: