Creating Simple:Press Plugins: Hello World!

Posted on Dec 22, 2018

Introduction

This is the first in a series of articles that will cover how to create Simple:Press plugins. It requires that you have some familiarity with creating and deploying standard WordPress plugins. If you understand the WordPress plugin building process then a Simple:Press plugin just requires building upon that knowledge.

Creating a Simple:Press plugin generally requires that you hook into the Simple:Press logic flow to display data or act on data as you need.

So, lets go ahead and build our first Simple:Press plugin!

Getting Started

First, you need to create a text file that forms the basis of your plugin. Lets call it sp-hello-world.php.

In that file you should first add a comment header that is very similar to a regular WordPress plugin header. The format and data in the comments tell Simple:Press and WordPress some critical data that bootstraps the plugin.

/*
Simple:Press Plugin Title: Hello World
Version: 1.0
Plugin URI: https://simple-press.com
Description: A Simple:Press plugin for displaying "Hello World" in specific locations (demo plugin)
Author: Andy Staines & Steve Klasen
Author URI: https://simple-press.com
Simple:Press Versions: 6.0 and above
$LastChangedDate: 2018-08-15 05:57:46 -0700 (Wed, 15 Aug 2018) $
$Rev: 15706 $
*/

With the above sample header, you indicate the user friendly name of the plugin, its version number, a website where the plugin can be found, the description that will be shown in the Simple:Press plugins page and so on.

When Simple press reads this file from the plugins directory, it will register the plugin and make it available for users to activate. Once activated, any code within the plugin will be executed on every page load. If there are no hooks into the WP or SP code rendering, nothing will occur for the plugin execution.

Make It Do Something

Right now all our file does is load up in the Simple:Press system but it doesn’t really do much. So lets make it do something now. Lets make it display HELLO WORLD in various locations around the forum.

First, we will define three places to display the phrase. These are defined using Simple:Press WordPress style action hooks:

add_action('sph_BeforeDisplayStart', 'sp_hello_display'); # before the forum display starts
add_action('sph_HeaderEnd', 'sp_hello_display'); # after the forum header section
add_action('sph_AfterDisplayEnd', 'sp_hello_display'); # after the forum display ends

The first one is used to output our string before the forum display begins. The second one will display the string after we have displayed our standard forum header. And finally the third one will display once all forum display has completed.

The standard WP API add_action() function adds a function to call, sp_hello_display(), when the listed action is fired.  In this case, for all three added actions, we will call our own function sp_hello_display() to write out our string.  This function looks like this:

function sp_hello_display() {
	echo '<p>Hello World...</p>';
}

The Full Plugin

Believe it or not, you have all the components now for a Hello World plugin! Here is the full code – you can copy and paste this into a text file, save it, zip it up and upload it to the Simple:Press Plugins screen!

<?php
/*
Simple:Press Plugin Title: Hello World
Version: 1.0
Plugin URI: https://simple-press.com
Description: A Simple:Press plugin for displaying "Hello World" in specific locations (demo plugin)
Author: Andy Staines & Steve Klasen
Author URI: https://simple-press.com
Simple:Press Versions: 6.0 and above
$LastChangedDate: 2018-08-15 05:57:46 -0700 (Wed, 15 Aug 2018) $
$Rev: 15706 $
*/

# dont allow calling the plugin file directly
if (preg_match('#'.basename(__FILE__).'#', $_SERVER['PHP_SELF'])) die('Access denied - you cannot directly call this file');

# need an action for where we want to hook into in order to display the Hello World string (3 examples)
add_action('sph_BeforeDisplayStart', 'sp_hello_display'); # before the forum display starts
add_action('sph_HeaderEnd', 'sp_hello_display'); # after the forum header section
add_action('sph_AfterDisplayEnd', 'sp_hello_display'); # after the forum display ends

# display Hello World string when actions fire
function sp_hello_display() {
	echo '<p>Hello World...</p>';
}

Wrap Up

There you have it. A very simple plugin to display the phrase Hello World in three locations. By using a standard WP or SP hook, there are literally hundreds of locations this could be done. This plugin simply highlights three of them.

We encourage you to peruse the Simple:Press core plugin code to find more action hooks where you might possibly display the Hello World phrase and add it to this plugin!