January 18, 2014

WordPress Plugin Boilerplate Templates

We do a considerable amount of development work at Athletics. As a result, we’re always looking for ways to make our work more portable and to create efficiencies that can be extended from project to project.

Over the holidays, I decided to put together a couple of boilerplate templates for WordPress plugins. The reasoning behind this is that, whenever I go to write a new plugin, I end up combing through repos of other projects trying to find something in the same vein to use as a jumping off point. One problem with this is that it can allow some outdated design patterns to creep in to new work. Another is that it simply wastes time a commodity that, as a new dad, I find precious.

Throughout 2013, I had been working towards taking an OOP approach to my WordPress work. Using a singleton design pattern for these templates, takes them, for me at least, to their logical conclusion as I don’t see the point of wrapping a set of functions in a class if the application will then be able to instantiate the class more than once.

Another thing I thought important was the ability to easily get the WordPress Settings API up and running quickly. I love the way in which the Settings API handles a lot of the heavy lifting of displaying fields and saving to the options table for you. However, given that the API is spread out across several functions, it’s sometimes difficult to wrap your head around.

To this end, the boilerplate template that implements the Settings API contains a set of variables in the constructor that, once defined, takes care of the bulk og getting the API up and running.

I think that’s enough writing on this one. I’ll let the code speak for itself. You can grab it from below or clone, fork, etc. the repo on Github. Get in touch via comments or email if you have some ideas on how I can improve these templates. Anyhow, I hope you find them useful.

WordPress Plugin Boilerplate with Settings

<?php
/*
Plugin Name: 
Plugin URI: 
Description: 
Version: 
Author: 
Author URI: 
License: GPL2
*/
 
/*  Copyright 20xx [name/orgainzation] ([email address])
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.
 
    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
*/
 
class CamelCaseClassName {
 
	private static $instance = false;
 
	public static function get_instance() {
	  if ( ! self::$instance ) {
	    self::$instance = new self();
	  }
	  return self::$instance;
	}
 
	private function __construct() {
		//Define plugin specific variables
 
		// add_options_page variables
		$this->options_page_title = '';
		$this->options_page_menu_title = '';
		$this->options_page_menu_slug ='';
 
		// add_settings_section variables
		$this->section_id = '';
		$this->section_title = '';
		$this->section_callback = '';
		$this->section_page = '';
 
		// register_setting/register_settings_fields variables
		$this->option_group = '';
 
		// Add action hooks
		add_action( 'admin_menu', array( $this, 'add_plugin_menu' ) );
		add_action( 'admin_init', array( $this, 'register_plugin_settings' ) );
	}
 
	public function add_plugin_menu() {
		// Add options page
		// http://codex.wordpress.org/Function_Reference/add_options_page
		add_options_page( 
			$this->options_page_title,
			$this->options_page_menu_title,
			'manage_options',
			$this->options_page_menu_slug,
			array( $this, 'render_options_page' )
		);
	}
 
	public function register_plugin_settings() {
 
		// Add settings section(s)
		// http://codex.wordpress.org/Function_Reference/add_settings_section
		add_settings_section(
			$this->section_id,
			$this->section_title,
			$this->section_callback,
			$this->plugin_slug
		);
 
		// Add settings field(s)
			// e.g. add_settings_field( $id, $title, $callback, $page, $section, $args );
		// http://codex.wordpress.org/Function_Reference/add_settings_field
 
		// Register plugin settings
		// register_setting( $option_group, $option_name, $sanitize_callback );
		// http://codex.wordpress.org/Function_Reference/register_setting		
	}
 
	public function example_section_callback() {
		echo 'Explain the settings here.';
	}
 
	public function render_options_page() {
	?>
		<div class="wrap">
			<h2><?php echo $this->options_page_menu_title; ?></h2>
			<form action="options.php" method="POST">
				<?php settings_fields( $this->option_group ); ?>
				<?php do_settings_sections( $this->options_page_menu_slug ); ?>
				<?php submit_button(); ?>
			</form>
		</div>
	<?php
	}
}
$underscore_class_name = CamelCaseClassName::get_instance();

WordPress Boilerplate Template without Settings

<?php
/*
Plugin Name: 
Plugin URI: 
Description: 
Version: 
Author: 
Author URI: 
License: GPL2
*/
 
/*  Copyright 20xx [name/orgainzation] ([email address])
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.
 
    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
*/
 
class CamelCaseClassName {
 
	private static $instance = false;
 
	public static function get_instance() {
	  if ( ! self::$instance ) {
	    self::$instance = new self();
	  }
	  return self::$instance;
	}
 
	private function __construct() {
 
	}
}
$underscore_class_name = CamelCaseClassName::get_instance();
Posted in: PHP | WordPress