Developing WordPress plugins using Pak Php framework - Part 1
Created On: 11 Apr, 2017: 12:21:36 - Tags : pak php framework | content management systems
IntroductionWordPress is a popular blogging and application platform. According to Wikipedia it is used by more than 60 million Web sites. More than 20% of the top 10 million websites are based on WordPress. There are over 40,000 plugins for WordPress which can be used to add all sorts of functionality. Plugin development for WordPress is very popular these days. Users with different levels of experience with Web development can easily create plugins for WordPress. The Pak Php Framework provides an object oriented interface to WordPress functions. It also provides built in utility classes, which simplify WordPress plugin development. In this article I will describe how to write a WordPress plugin using the Pak Php Framework using an example project called wordpressexample that is included with the Pak Php Framework.
Basic Structure of a WordPress PluginA WordPress plugin has a simple structure. It requires just one Php file. It can of course include more files. This file can have any name but usually the name is derived from the plugin name. For example, if your plugin is called Hello World, then your plugin file can be hello-world.php. The plugin files can contain object oriented or procedural code. There is no restriction on the structure of the code. If you want your plugin to be hosted on wordpress.org, then your plugin should also have a readme.txt file. This file contains information that is read by the WordPress plugin repository and is used to host your plugin. You can also choose to not host your plugin on wordpress.org. The main function of a WordPress plugin is to extend the functionality of WordPress using hooks. A hook is a function defined in the WordPress core, that extends the functionality of WordPress. A WordPress plugin can register functions with WordPress hooks. These functions are run at certain times. There are 2 types of hooks. First one is called filter. A filter hook allows filtering content. For example the filter hook called the_title allows editing the title of a post. Before WordPress displays the title of the post, it runs each function that is registered with the_title hook. Each function can modify the title of the post. The end result is displayed as the post title by WordPress. The second type of hook is called action. An action hook allows running functions at certain times. For example the wp_footer action hook allows displaying content when the footer of the page is displayed. The functions that are registered with the wp_footer hook are called when wp_footer function is called inside a WordPress theme. It depends on the developer when he wants to call this function, but usually it is called inside the footer of a theme. All functions that register with a hook have to be valid Php callback functions. Registering actions and filters with WordPress is the most common function of WordPress plugins. WordPress also provides built in functions that allow access to WordPress features. For example the WP_Query class allows us to search for posts using different search criteria.
How to Write your WordPress Plugin using the wordpressexample projectThe Pak Php framework contains an example project called wordpressexample. This is a simple WordPress plugin that adds a dashboard widget and a settings page. The settings page allows the user to set the title and content of the Dashboard widget. To run the wordpressexample project, you have to download all files in the Pak Php package. Then you have to upload the files to the plugin folder of your WordPress installation. After that copy the file index.php from wordpressexample folder to the root of the plugin folder. Then you have to activate the plugin from the WordPress plugin menu. You can then customize the plugin by editing the files in the wordpressexample folder. The Pak Php folder has following contents:
- framework: This contains the Pak Php framework files
- webexample: This contains a sample web application based on the Pak Php framework. It contains a test method that allows unit testing the WordPress plugin
- wordpressexample: This contains a sample WordPress plugin
- autoload.php: This file is used to auto load Php classes
- index.php: This is the entry point for all application requests
The wordpressexample folder has following contents:
- css: It contains CSS files used by the plugin
- language: It contains translation files used by the plugin
- templates: It contains html template files used by the plugin
- vendors: It is used to hold third party Php scripts
- Configuration.php: It is the configuration file for the plugin. The structure of this file is described in the next section
- DashboardWidget.php: It is used to construct the Dashboard widget
- README.txt: This is the WordPress plugin README.txt file. It allows your project to be hosted on wordpress.org
- Settings.php: It is used to construct the settings page for the plugin
- Testing.php: It extends the WordPress XML-RPC interface. It allows unit testing the plugin
- WordPressExample.php: This is the main plugin file
Configuration settings for wordpressexample projectThe Pak Php framework has a configuration file that contains default settings for WordPress projects. The Configuration.php file of the wordpressexample project overrides these default settings. Each configuration option is documented in the Configuration.php file. The file contains following sections:
- Plugin information: This section specifies the plugin name and plugin version
- Test parameters: This section contains configuration related to test parameters. The main parameters in this section are:
- test_mode: This indicates if the application is in test mode
- test_type: This indicates the type of testing. e.g unit,functional or script. The wordpressexample plugin implements unit tests
- test_classes: This indicates the list of classes that will be unit tested
- development_mode: This indicates if the plugin is in development mode
- Required framework classes: This section indicates which classes are required by the plugin. These classes are auto loaded. The class list can include classes from the utilities package or user defined classes.
- Configuration parameters for test mode: This section contains parameters that are included only if the application is in test mode.
- Configuration parameters for live mode: This section contains parameters that are included only if the application is not in test mode. It contains parameters that define the settings page.