Pak Jiddat

Read: In the name of thy Lord Who createth, Createth man from a clot. Read: And thy Lord is the Most Bounteous, Who teacheth by the pen, Teacheth man that which he knew not. Nay, but verily man is rebellious That he thinketh himself independent!. Lo! unto thy Lord is the return. (Sura Alalaq 96:8)

Developing WordPress plugins using Pak Php framework - Part 1

Created On: 11 Apr, 2017: 12:21:36 - Tags : pak php framework | content management systems

Introduction

WordPress 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 Plugin

A 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 project

The 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:
  1. framework: This contains the Pak Php framework files
  2. 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
  3. wordpressexample: This contains a sample WordPress plugin
  4. autoload.php: This file is used to auto load Php classes
  5. index.php: This is the entry point for all application requests
The wordpressexample folder has following contents:
  1. css: It contains CSS files used by the plugin
  2. js: It contains JavaScript files used by the plugin
  3. language: It contains translation files used by the plugin
  4. templates: It contains html template files used by the plugin
  5. vendors: It is used to hold third party Php scripts
  6. Configuration.php: It is the configuration file for the plugin. The structure of this file is described in the next section
  7. DashboardWidget.php: It is used to construct the Dashboard widget
  8. README.txt: This is the WordPress plugin README.txt file. It allows your project to be hosted on wordpress.org
  9. Settings.php: It is used to construct the settings page for the plugin
  10. Testing.php: It extends the WordPress XML-RPC interface. It allows unit testing the plugin
  11. WordPressExample.php: This is the main plugin file

Configuration settings for wordpressexample project

The 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:
  1. Plugin information: This section specifies the plugin name and plugin version
  2. 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
  3. 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.
  4. Configuration parameters for test mode: This section contains parameters that are included only if the application is in test mode.
  5. 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.

How to Load Custom JavaScript and CSS Files

To load your JavaScript and CSS files you just have to upload your files to your plugin folder and mention them in the Configuration.php file. The CSS and JavaScript files that should be loaded on the public face of your Web site should be given in the parameters: ['wordpress']['public_scripts']  and ['wordpress']['public_styles']. The CSS and JavaScript files that should be loaded on the admin pages are given in the parameters:['wordpress']['admin_scripts'] and ['wordpress']['admin_styles']. Each JavaScript file is given by a single array element. It should have following keys: name, which is the name for the script file. file, which is the path to your JavaScript file relative to your plugin root directory.dependencies, which is an array of scripts that the given script depends on. The last parameter is localization. It is an optional parameter that contains translation information for the JavaScript file. Each CSS file has the same format. It has an addition field called media which is the media attribute for the CSS file.

Conclusion

This part of the tutorial described how to develop a basic WordPress plugin using Object Oriented programming using the Pak Php Framework. In the next part I will describe how to implement advanced WordPress plugin features such as plugin translation, dashboard widgets, custom posts and taxonomies, plugin settings and unit testing.

Related Articles

  1. Managing Php scripts using the Pak Php Framework Admin Tool
  2. Developing WordPress plugins using Pak Php framework - Part 2
  3. How to test Pak Php applications
  4. Writing command line scripts using Pak Php framework
  5. Pak Php framework
  6. Developing your own Php Utilities Framework