Plugin base

After having installed the SDK and perhaps even including it in your Composer, it is time to write the WordPress plugin you want.

A basic example

Below is an example plugin base off of the Plainview SDK WordPress class.

<?php
/*
Author:			Edward Plainview
Author Email:	plugins@plainview.se
Author URI:		https://plainviewplugins.com
Description:	Testing the Plainview SDK WordPress class.
Plugin Name:	Test SDK plugin
Plugin URI:		https://plainviewplugins.com/
Version:		1
*/

require_once( 'vendor/autoload.php' );

class TestPlugin
	extends \plainview\sdk_test23\wordpress\base
{
	public function _construct()
	{
		// Do things here.
	}
}
$TestPlugin = new TestPlugin();

The above is about as basic a plugin as can be. The comment is the necessary WordPress plugin information, followed by loading the Composer autoloading scripts.

After that comes the name of your plugin class, which is an extension of the SDK WordPress base class. Note the use of the customized namespace.

Then comes the constructor which you’ll notice has only one underscore, while PHP normally uses two. This is the first you’ll encounter of the Plainview SDK in your code. Thre reason for this single-underscored constructor is because the SDK is using the double-underscored constructor to prepare the class with lots of WordPress information. Have a look at the plainview/sdk/wordpress/base.php file. You’ll see lots of path saving, hooking and saving of convenience variables. That constructor will automatically run the constructor with the single underscore, which is where you put your code.

Finally your plugin is instanciated. That’s all you need to start!

More detailed, more organised: Broadcast

The above example is very simple and does not follow the best coding routines. There is no namespacing, no separation of WordPress plugin and actual SDK class, no convience method to return your plugin’s instance, among other things. Your plugin, if used and developed often, will gradually increase in size and complexity which will require you to break it up into smaller pieces and add efficient ways of navigating your code and plugin methods.

Now would be a good time to download the ThreeWP Broadcast plugin and take a look at the code. It shows what the SDK can do:

  • There is a small WordPress plugin…
  • … that loads the Broadcast class proper.
  • Composer takes care of class autoloading.
  • The plugin uses a general namespace in order to provide the ThreeWP_Broadcast() method that returns the instance of Broadcast.
  • Broadcast itself has its own namespace.
  • Traits split up code into categorized pieces.
  • The _constructor shows many examples of WordPress action hooking.
  • Broadcast uses many features of the SDK: forms, tables, tabs, actions.

Base documentation

This page cannot go into details regarding the WordPress base class. This is because it inherits many methods from the SDK base class and many methods are not too interesting. See the SDK documentation index for the most interesting helper classes and methods. Some things from the WordPress base class  should be noted here:

Paths

Queries

 

Leave a Reply

Your email address will not be published. Required fields are marked *