Writing plugins using object-oriented PHP
So far, all the plugin examples that have been covered in this chapter have been written using the procedural PHP programming style. In this style, all functions are declared directly in the main body of the plugin and the hook registration functions have direct access to these functions.
WordPress plugins can also be written using an object-oriented PHP approach. This recipe shows how the code from the previous recipe can be restructured to be written in object-oriented PHP.
Getting ready
You should have already followed the Loading a style sheet to format plugin output recipe to have a starting point for this recipe. Alternatively, you can download the resulting code (ch2/ch2-private-item-text/ch2-private-item-text-v2.php
) for that recipe from the book's GitHub page.
How to do it...
Follow these steps to transform an existing plugin's code into object-oriented PHP:
- Log in to the administration page of your WordPress installation.
- Click on Plugins in the left-hand navigation menu.
- Check whether the Chapter 2 - Private Item Text plugin is currently active and Deactivate it if it is.
- Copy the entire contents of the
ch2-private-item-text
directory and rename the copych2-oo-private-item-text
. - Navigate to the newly renamed folder and rename the main PHP code file
ch2-oo-private-item-text.php
. - Open the newly renamed plugin file in a code editor.
- Update the plugin header to change the name of the plugin to
Chapter 2 - Object-Oriented - Private Item Text
. - Right after the plugin header, add the following text to declare a new class for the plugin and specify a constructor method for this class:
class CH2_OO_Private_Item_Text { function __construct() { } } $my_ch2_oo_private_item_text = new CH2_OO_Private_Item_Text();
- Move the calls to the
add_shortcode
andadd_action
functions to be placed inside of the class constructor method (__construct
). - Modify the second argument of the
add_shortcode
andadd_action
functions as follows:add_shortcode( 'private', array( $this, 'ch2pit_private_shortcode' ) ); add_action( 'wp_enqueue_scripts', array( $this, 'ch2pit_queue_stylesheet' ) );
- Move the complete
ch2pit_private_shortcode
andch2pit_queue_stylesheet
functions inside of the class body (after the__construct
method and before the class closing bracket). - Save and close the modified file.
- Log in to the administration page of your development WordPress installation.
- Click on Plugins in the left-hand navigation menu.
- Activate the new plugin.
- Visit your site to see that the private item content functionality is still in place and works as it did before.
How it works...
The code changes that we applied to the plugin first declare a class for all of our plugin's functionality and also contain a constructor method for that class. The __construct
method is called once, as soon as the class is instantiated by the last line in the plugin's code, and can be used to associate custom functions with all action hooks, filter hooks, and shortcodes.
The main benefit of using an object-oriented approach is that you don't have to be as careful when naming your hook callbacks and all other functions, since these names are local to the class and can be the same as function names declared in any other classes or in procedural PHP code.
There's more…
If you enjoy object-oriented plugin development and create a lot of plugins, you might benefit from using a boilerplate generator.
WordPress plugin boilerplate generator
By visiting the WordPress plugin boilerplate generator (https://wppb.me/), you can easily create code that needs to be written each time you create a plugin. After entering basic data about your plugin, you will receive a download with the core structure for your new plugin. This template contains a number of object-oriented concepts that are best suited to developers who are well versed in object-oriented programming.
See also
- The Creating a new enclosing shortcode recipe