Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example of bootstrapping a WordPress plugin.

Example of bootstrapping a WordPress plugin.

This is a (rather useless) example of how I typically bootstrap most of my WordPress plugins.

A lot of my WordPress plugins are a combination of a generalised, reusable PHP library (that may be separated into its own Composer package), and a WordPress integration plugin wrapping that reusable library.

This means that there are several possible ways of interacting with that library, which means that I also have as many different bootstrapping paths. For unit testing, for example, I don't use the WordPress plugin wrapping the library, but rather instantiate the library directly to test it in isolation.

All of these have in common that my objects are configurable through Config files, and a very simple way of having them behave differently for each bootstrapping path is to have different configuration files.

This gives me lots of flexible, and is one of the best ways of creating more reusable code that you can grow from project to project.

<?php
// file: src/Controller.php
/**
* Example Plugin Controller.
*/
namespace Example\Plugin;
use BrightNucleus\Config\ConfigInterface;
use BrightNucleus\Config\ConfigTrait;
use BrightNucleus\Exception\RuntimeException;
/**
* Class Plugin
*
* @since 1.0.0
*
* @package Example\Plugin
* @author Alain Schlesser <alain.schlesser@gmail.com>
*/
class Controller {
// See: https://github.com/brightnucleus/config
use ConfigTrait;
/**
* Instantiate a Plugin object.
*
* @param ConfigInterface $config Config to parametrize the object.
* @throws RuntimeException If no valid Config was given.
*/
public function __construct( ConfigInterface $config ) {
$this->processConfig( $config );
}
/**
* Launch the initialization process.
*/
public function init() {
add_filter( 'wp_title', [ $this, 'filter_wp_title' ], 10, 2 );
}
/**
* Creates a nicely formatted and more specific title element text
* for output in head of document, based on current view.
*
* @param string $title Default title text for current view.
* @param string $sep Optional separator.
* @return string Filtered title.
*/
public function filter_wp_title( $title, $sep ) {
// Use the configuration data.
return sprintf( $this->getConfigKey( 'title_pattern' ), $title, $sep );
}
}
<?php
// file: example-plugin.php
/**
* Example Plugin.
*
* @wordpress-plugin
* Plugin Name: Example Plugin
* Plugin URI: https://www.alainschlesser.com/
* Description: Handles all interactions with the membership aspects of the site, including members, products and orders.
* Version: 1.0.0
* Author: Alain Schlesser
* Author URI: http://www.alainschlesser.com/
* Text Domain: example-plugin
* Domain Path: /languages
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
*/
namespace ExamplePlugin;
// See: https://github.com/brightnucleus/config
use BrightNucleus\Config\ConfigFactory;
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
// Vendor/package hierarchy prefix in config file.
const CONFIG_PREFIX = 'Example\Plugin';
// Load Composer autoloader.
if ( file_exists( __DIR__ . '/vendor/autoload.php' ) ) {
require_once __DIR__ . '/vendor/autoload.php';
}
// Fetch WordPress-specific configuration settings.
$config = ConfigFactory::create( __DIR__ . '/config/wp_defaults.php' );
// Register Example\Plugin\Controller and hook it up to WordPress.
$example_plugin = new Controller( $config->getSubConfig( CONFIG_PREFIX, 'Controller' ) );
add_action( 'init', [ $example_plugin, 'init' ] );
<?php
// file: config/wp_Defaults.php
/**
* Example Plugin WordPress-specific configuration.
*/
namespace Example\Plugin;
/*
* Controller configuration.
* (stupid example to demonstrate the use of the configuration file)
*/
$controller_config = [
// %1$s = $title
// %2$s = $separator
'title_pattern' => 'Cool web page named %$1s %$2s Example Plugin';
];
return [
// Vendor
'Example' => [
// Package
'Plugin' => [
// Component
'Controller' => $controller_config,
],
],
];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.