WP MoJ Components
WordPress plugin that applies common functionality to sites used across the MoJ portfolio.
by Ministry of Justice · github.com/ministryofjustice/wp-moj-components · website
Install
No release zip yet. The repository archive installs, but the folder name will carry the branch suffix and updates will not flow:
wp plugin install https://github.com/ministryofjustice/wp-moj-components/archive/refs/heads/main.zipA plugin to manage commonly used functionality across the MoJ portfolio of WordPress properties.
Why?
- Simple and efficient maintenance approach
- Enforces a baseline standard.
- Centralised repo for common website functions
- Time-saving development method due to 'one fix affects all'
- Increased website load performance
Components within this plugin attempt to create a baseline standard that all MoJ WP properties can benefit from. With ease of component authoring and maintenance it is the preferred method for global code inclusion.
Current components
-
Introduce: creates a Dashboard widget containing contact information so stakeholders can get support
-
Introduce (popup): provides a popup banner to relay administrative information to stakeholders
-
Multisite: adds support for multisite based environments
-
Multisite (domain tracker): a non destructive custom domain tracker, provided primarily for the WASM tool.
-
Security: applies scanning functionality and various security patches
-
Security (rest-api): prevents access to specified REST API routes
-
Sitemap: provides auto-generated sitemap functionality and shortcodes
-
Users: introduces user-switch capability for admins plus monitors inactive users for GDPR
-
Versions: grants access to WordPress version over API and provides information relating to installed plugin and theme versions
How to install
Require the plugin in your composer.json file and always use the latest version:
"ministryofjustice/wp-moj-components": "*"
Load the plugin in the mu-plugins directory when you run composer update.
How it works
Inside the directory named 'component' are sub-directories that represent individual functionality. These are called 'components'.
Each component is stand-alone and contains its own assets and code. Components load automatically using the composer PSR4 autoloader.
Other conventions include:
- Asset directory can contain:
css/fonts/images/js/
- Components can store sub-classes in a directory called
sub/for auto-loading
You have access to a Helper class within the component directory that provides solutions to paths and offers an easy way to keep inter-common code and functions organised.
You can access this class and it's methods from any component with the following function call:
Helper()->method()
Adding a component
Prerequisite
Please bear in mind that adding a component will affect every site that uses this plugin.
To ensure best fit and before you begin; can you agree with the following statements?
- The component I am writing should be used by all websites
- The component will benefit all our users and stakeholders
- The component helps solve a general business requirement
Setup
Create your component using the following structure.
Feature.php is the main class in the component namespace that handles all the work
-
component/Feature -
component/Feature/Feature.php -
an asset directory for use by this component only
-
component/Feature/assets -
component/Feature/assets/css -
component/Feature/assets/fonts -
component/Feature/assets/images -
component/Feature/assets/js -
an autoload sub-directory for assistive functionality
-
component/Feature/sub -
component/Feature/sub/Class.php
Example: basic component class
<?php
namespace MOJComponents\Feature;
/**
* A great description about what this feature does
*/
class FeatureClass
{
public function __construct()
{
$this->actions();
}
private function actions()
{
// hooks in here
}
public function method()
{
// do some work
}
public function another_method()
{
// do some other work
}
}