120 The Plugin Class (How-to Video)
120 The Plugin Class (Video)

Part of the “How to Write a WordPress Plugin” series
This Lesson – Overview
In previous lessons we covered the introduction to plugins and the naming and declaration of a WordPress plugin.
In this lesson we’ll introduce our WordPress plugin class. This class can be used as a basis for all your plugins, but more on that later.
Well also cover some WordPress recommendations and general ideas about plugin development, plus several tasks our plugins will need to accomplish when interfacing with WordPress.
- Plugin’s can be developed using two method;
- using individual functions or by
wrapping the functions in a class using an object-oriented approach.
In these tutorials we will be using the latter method and developing our own plugin class.
Contents
- 110 WordPress Plugin Naming and Declaration (How-to Video)
- 120 The Plugin Class (How-to Video)
- 130 WordPress Hooks, Actions and Filters (How-to Video)
- 131 Text to Hyperlink Filter Example (How-to Video)
- 140 WordPress Administration Menus/Panels/Pages (How-to Video)
- 141 WordPress PK Admin Menu 0v1 Demo – A Simple Sub Menu (How-to Video)
- 142 WordPress PK Admin Menu 0v2 A Top-level and Sub-level Menu (How-to Video)
- 143 Admin Menu 0v3 Text to Hyperlink (How-to Video)
- 150 Database
- 161 Javascript Inclusion with Plugins
- 162 CSS Inclusion with Plugins
- 170 Plugin Release and Promotion
WordPress Coding Standards
First a quick word about the WordPress coding standards.
WordPress is working to improve its coding standards by helping users maintain a consistent style so everyone’s code can be clean and easy to read at a glance. The full WordPress coding standards can be found at the WordPress codex, http://codex.wordpress.org/WordPress_Coding_Standards
Some of the main points worth noting are:
- NoCammelCaseText to be used.
- Never use shorthand PHP start tags. Always use full PHP tags.
Correct:
<?php ... ?>
Incorrect:
<? ... ?> <?=$var?>
- Remove all trailing whitespace after closing PHP tags.
- Remove trailing spaces at the end of each line of code.
- Use lowercase letters in variable and function names with words separated using underscores.
function some_name( $some_variable ) { [...] }
- Files, folders or directories should be named descriptively using lowercase letters with words separated using hyphens.
my-plugin-name.php
- Class file names should be based on the class name with class- prepended and the underscores in the class name replaced with hyphens:
Declaration:
class wp_xmlrpc_server {
…
} // End class wp_xmlrpc_server
Filename:
class-wp-xmlrpc-server.php
- Don’t use “clever coding“, readability is more important than cleverness or brevity.
NO
isset( $var ) || $var = some_function();
YES
if ( ! isset( $var ) ) $var = some_function();
- Use tabs and not spaces for indentation.
A Plugin Class
OK, lets get our feet wet, we’ll begin by creating a plugin class structure to hold our code.
Why have a class structure rather than just PHP files? Well, having a PHP Class avoids all the problems we could have from naming collisions with other WordPress plugins and our own code.
So to avoid naming collisions, all plugins should incorporate a PHP Class structure.
Before we begin, I’ll admit the following class does not hold all the answers. I don’t know all the answers. So I welcome any suggestions for improving on these ideas.
Here is the outline for code we will be using to set-up our plugin class structure.
/**
* The Plugin Class
*/
if (!class_exists(”pk_plugin_template_class”)) {
class pk_plugin_template_class {
// TODO: Variable declarations go here
// TODO: Constructor
function __construct {
// TODO: Enter Plugin Actions and Filters here
} // End constructor
// Core functions go here
// TODO: Enter Action and Filter Methods here
// TODO: Enter Helper Methods here
} // End pk_plugin_template_class
} // End if (!class_exists(”pk_plugin_template_class”))
The above code first checks for the existence of a class named pk_plugin_class and if the class doesn’t exist, the class is created.
Notice the placeholders within the class for;
- variable declarations,
- class constructor,
- other functions,
we will be using these placeholders later.
Most IDE’s have a function that lists all outstanding TODO’s, so I place them throughout the code to easily locate what needs to be done during development. Note that there are three primary areas of code:
- Constructor. This function is responsible for defining any constants used throughout the code, specifying any localization files, and registering all actions and filters with WordPress.
- Core Functions are the actual function definitions registered in the constructor that are fired after during WordPress’ execution.
- Helper Functions are functions that help in execution by abstracting away common functionality (such as registering JavaScripts and stylesheets).
We next need to instantiate (create an instance of) the class. This code checks that the class is available and then creates an instance of the class.
/**
* Instantiate (create an instance of) the class
*/
if (class_exists(”pk_plugin_template_class”)) {
// TODO: Enter New Instance Name
$pk_plugin_demo = new pk_plugin_template_class();
} // End instantiate class
If we were to activate the plugin now, an instance of our class would be created, but of course nothing would happen as we haven’t told it to do anything. That’s where actions and filters come into the equation.
OK, lets add our plugin class to our plugin file.
Open the pk-plugin-class.php file in the wp-content\plugins\pk-plugin-class\ folder, using your favourite text editor. I’m using the PDT Eclipse IDE.
Then append our plugin class to the end of the file. I’m just going to use cut and past to save time. If you need the header code, you will find its details in the previous lesson.
Now that we have our class, lets check everything is working by entering the customary “Hello World” code.
The first thing we need to do is enter the hello world function. This would be classed as a core function so should be placed after the comment;
// TODO: Enter Actions and Filter Method here
The hello world function we are using for our test will do nothing more than output the text;
<– PK Plugin Class Test – Hello World–>
at the top of any page using the WordPress init hook.
http://codex.wordpress.org/Plugin_API/Action_Reference/init
Next we need to enter the WordPress init action hook that will call our Hello Word function. This is a WordPress action hook so needs to be placed after the comment,
// TODO: Enter Plugin Actions and Filters Here
And that’s everything we need for a fully functional plugin.
Save the file, and let go see if it works. Open your browser and login as administrator to WordPress.
We need to activate our plugin, so select the Installed Plugins hyperlink, locate our plugin and select the Activate hyperlink.
Presuming WordPress shows no errors, our text
<– PK Plugin Class Hello World –>
Should be shown at the top of every WordPress page that calls the init action hook.
Lets take a look at our blog. Click the blog page hyperlink, and at the top of the page should be the text
<– PK Plugin Class Hello World –>
Well everything seems to be working, so lets deactivate the plugin. Then remove the, Hello World, code from our plugin’s PHP file. This code will no longer be required. And save the file ready for our next lesson.
Conclusion – Summary
Well, that’s all for the tutorial on creating a WordPress plugin Class, see you in the next lesson when we will start implementing more plugin hooks, actions and filters.
Lesson Code – Plugin Class
<?php
/*
Plugin Name: PK Plugin Class
Plugin URI: http://kingsolutions.org.uk/wordpress/wordpress/how-to-write-a-wp-plugin/
Description: A Simple Plugin Class for WordPress
Version: 0.1
Author: Philip King
Author URI: http://kingsolutions.org.uk/wordpress/
Licence: Licence GPL2
*/
/* Copyright 2011 Philip King (contact: http://kingsolutions.org.uk/wordpress/)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* The Plugin Class
*/
if (!class_exists("pk_plugin_template_class")) {
class pk_plugin_template_class {
// TODO: Variable declarations go here
// TODO: Constructor
function __construct() {
// TODO: Enter Plugin Actions and Filters here
} // End constructor
// Core functions go here
// TODO: Enter Action and Filter Methods here
// TODO: Enter Helper Methods here
} // End pk_plugin_template_class
} // End if (!class_exists("pk_plugin_template_class"))
/**
* Instantiate (create an instance of) the class
*/
if (class_exists("pk_plugin_template_class")) {
// TODO: Enter New Instance Name
$pk_plugin_demo = new pk_plugin_template_class();
} // End instantiate class
?>
Lesson Code – Hello World Demo
<?php
/*
Plugin Name: PK Plugin Class
Plugin URI: http://kingsolutions.org.uk/wordpress/wordpress/how-to-write-a-wp-plugin/
Description: A Simple Plugin Class for WordPress
Version: 0.1
Author: Philip King
Author URI: http://kingsolutions.org.uk/wordpress/
Licence: Licence GPL2
*/
/* Copyright 2011 Philip King (contact: http://kingsolutions.org.uk/wordpress/)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details. You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* The Plugin Class
*/
if (!class_exists("pk_plugin_template_class")) {
class pk_plugin_template_class {
// TODO: Variable declarations go here
// TODO: Constructor
function __construct() {
// TODO: Enter Plugin Actions and Filters here
// This calls the hello_world() function when WordPress inintializes.
add_action('init', array($this, 'hello_world'),1);
} // End constructor // Core functions go here
// TODO: Enter Action and Filter Methods here
function hello_world() {
echo "<-- PK Plugin Class Test - Hello World -->";
} // End hello_world()
// TODO: Enter Helper Methods here
} // End pk_plugin_template_class
} // End if (!class_exists("pk_plugin_template_class"))
/**
* Instantiate (create an instance of) the class
*/
if (class_exists("pk_plugin_template_class")) {
// TODO: Enter New Instance Name
$pk_plugin_demo = new pk_plugin_template_class();
} // End instantiate class
?>





For some reason, all of your videos open except 120 The Plugin Class (Video)
Hi Jerome
I’ve just checked the video, 120 The Plugin Class (How-to Video), and everything seems to be working fine. Maybe YouTube was a bit busy?
If you are still having a problem, please let me know and I will look into the matter further.