WordPress plugin development
Basic Knowledge
We assume that you are familiar with object-oriented PHP programming. You should also have knowledge of HTML, JavaScript, JQuery, and CSS. WordPress is a quality CMS, offering many facilities for the development of extensions. However, it is important to master at least the basics, and to know what are for example the actions, the filters, and in which order they are called. Indeed, a WordPress extension requires registering as a listener to a series of hook calls to integrate into its operation.
If you’re not comfortable with the basics of WordPress development, you should take the time to check out these links:
You will then appreciate all the more the simplicity brought by the Sukellos Fw ;)
Sukellos Basic Plugin
In order to save as much time as possible, we have designed for you a very simplified plugin template: Sukellos Basic Plugin. This plugin inherits Sukellos Fw, giving you an easy example of how to make you own. It consists of a very basic administration page, and a “Hello World!” shortcode.
You can download Sukellos Basic Plugin here.
This plugin can be activated directly as is.
Once activated, you can see the administration page, built with the Sukellos Admin Builder plugin:
If you copy/paste this shortcode on any post, or pages, we will see a Hello World! text displayed. This shortcode is just a basic example of how to implement it with Sukellos Fw.
The following screenshot shows you the structure of this plugin.
The Sukellos Basic plugin is located under a directory named wp-sukellos-basic-plugin.
Its main file is therefore logically named wp-sukellos-basic-plugin.php, containing our first class, called WP_Sukellos_Basic_Plugin_Loader, whose responsibility is the loading of the plugin and its administration:
- WP_Sukellos_Basic_Plugin, which is the plugin class, located in class-wp-sukellos-basic-plugin.php
- WP_Sukellos_Basic_Plugin_Admin, which is the admin plugin class, located in class-wp-sukellos-basic-plugin-admin.php
Now have a look at the class diagram:
In this class diagram, you can see that each of our class extends another parent class located in Sukellos Fw. This trick is enabled by inheritance in object-oriented programming. This allows us to group all the reusable logic in the parent class, and thus only have to worry about the specific processing that interests us.
The Loader
This loader is both simple and powerful. It does all the work of integrating and interacting with WordPress for you.
The only things you have left to do are tell him:
- The class that manages the plugin
- The class that build the administration (is needed)
- The action links that will be displayed in the Plugins area of WordPress (settings, documentation…)
The file header contains all the plugin information, in compliance with WordPress standards, which you just need to specify for your own needs.
As we saw in the class diagram, the loader WP_Sukellos_Basic_Plugin_Loader extends the WP_PLoad parent class. WP_PLoad is an abstract class that will force the implementation of a few methods :
- get_plugin() is used to return the reference to the plugin instance
- get_plugin_admin() is used to return the reference to the admin instance
- get_plugin_file() is the way to allow the WP_PLoad parent to retrieve plugin header information
All other methods are used to give the action links :
- get_update_url() is the URL used by WordPress to update the plugin
- get_settings_url() is the link for Settings
- get_documentation_url() is the link for Documentation
- get_support_url() is the link for Support
- get_sales_page_url() is the link for More plugins
Returning a blank value will disable the link display.
The use Singleton; line is a shortcut managed by Sukellos Fw. It makes it very easy to specify a class as a Singleton.
The Plugin
The plugin WP_Sukellos_Basic_Plugin extends the WP_Plugin parent class. WP_Plugin is an abstract class too, that will force the implementation of 2 methods :
- init() that can be used to launch our specific functionalities, as a shortcode for example
- init_custom_post_types() that must be used to create your custom post types, if you have some.
This last method was introduced to allow Sukellos Fw to properly manage the flushing rewrite that must be done when activating the plugin.
The Admin
And finally, the admin. Those of you who have already implemented an administration page under WordPress will greatly appreciate the simplicity provided by the Sukellos Admin Builder plugin, which is part of the Sukellos Fw… and which is moreover the most powerful part of it. This admin relieves you of all the tedious part, and will seem disconcertingly simple. That’s what you could call magic ;)
The admin class WP_Sukellos_Basic_Plugin_Admin extends the WP_Plugin_Admin parent class. WP_Plugin_Admin is an abstract class, that will manage the Admin Builder integration, and force the implementation of only 2 methods :
- admin_enqueue_scripts() that can be used to register some CSS or JS files, but it’s optional
- create_fields() which contains the instructions for creating the admin page, its content, and especially all its fields (options).
In this basic plugin, the admin page contains only simple content.
In a more advanced plugin, you could create and all types of options, user_meta, post_meta, AJAX actions, CSS automation… allowing you all the fields you need, as simply as possible, thanks to the power of the Admin Builder.
The use of this plugin is detailed in a dedicated tutorial