Book Image

WordPress Plugin Development Cookbook - Third Edition

By : Yannick Lefebvre
Book Image

WordPress Plugin Development Cookbook - Third Edition

By: Yannick Lefebvre

Overview of this book

WordPress is one of the most widely used, powerful, and open content management systems (CMSs). Whether you're a site owner trying to find the right extension, a developer who wants to contribute to the community, or a website developer working to fulfill a client's needs, learning how to extend WordPress' capabilities will help you to unleash its full potential. This book will help you become familiar with API functions to create secure plugins with easy-to-use administration interfaces. This third edition contains new recipes and up-to-date code samples, including new chapters on creating custom blocks for the block editor and integrating data from external sources. From one chapter to the next, you’ll learn how to create plugins of varying complexity, ranging from a few lines of code to complex extensions that provide intricate new capabilities. You'll start by using the basic mechanisms provided in WordPress to create plugins, followed by recipes covering how to design administration panels, enhance the post editor with custom fields, store custom data, and even create custom blocks. You'll safely incorporate dynamic elements into web pages using scripting languages, learn how to integrate data from external sources, and build new widgets that users will be able to add to WordPress sidebars and widget areas. By the end of this book, you will be able to create WordPress plugins to perform any task you can imagine.
Table of Contents (15 chapters)

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:

  1. Log in to the administration page of your WordPress installation.
  2. Click on Plugins in the left-hand navigation menu.
  3. Check whether the Chapter 2 - Private Item Text plugin is currently active and Deactivate it if it is.
  4. Copy the entire contents of the ch2-private-item-text directory and rename the copy ch2-oo-private-item-text.
  5. Navigate to the newly renamed folder and rename the main PHP code file ch2-oo-private-item-text.php.
  6. Open the newly renamed plugin file in a code editor.
  7. Update the plugin header to change the name of the plugin to Chapter 2 - Object-Oriented - Private Item Text.
  8. 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();
  9. Move the calls to the add_shortcode and add_action functions to be placed inside of the class constructor method (__construct).
  10. Modify the second argument of the add_shortcode and add_action functions as follows:
    add_shortcode( 'private', array( $this,
        'ch2pit_private_shortcode' ) );
    add_action( 'wp_enqueue_scripts', array( $this,
        'ch2pit_queue_stylesheet' ) );
  11. Move the complete ch2pit_private_shortcode and ch2pit_queue_stylesheet functions inside of the class body (after the __construct method and before the class closing bracket).
  12. Save and close the modified file.
  13. Log in to the administration page of your development WordPress installation.
  14. Click on Plugins in the left-hand navigation menu.
  15. Activate the new plugin.
  16. 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