wordpress-mu-common-toolkit
WordPress Common Toolkit MU Plugin
A simple MU plugin for WordPress that adds functionality that I use on web site projects, including a configuration registry.
Installation
Simply copy the common-toolkit.php
file to your wp-content/mu-plugins
directory (create one if it does not exist).
Requirements
- PHP 5.4+ (via JSON config file) and PHP 7.x (via array or JSON file)
- WordPress 4.7 or higher
Configuration
All variables are optional.
Variable
Description
Type
Default
environment
Environment of current instance (ex: ‘production’, ‘development’, ‘staging’)
string
“production”
environment_constant
Constant used to determine environment, environmental variable name for getenv()
.
string
“WP_ENV”
environment_production
The label used to match if production environment.
string
“production”
admin_bar_color
Change admin bar color in current environment
string
null
disable_emojis
Remove support for emojis
bool
false
disable_php_update_nag
Removes the “PHP Update Required” dashboard widget/notice
bool
false
disable_search
Disable WordPress site search
bool
false
disable_site_health
Disable site health notifications, widgets and menu
bool
false
disable_updates
Disable WordPress core, plugin and/or theme updates. Values: core, plugin, theme; true
for all
bool/string/array
false
disable_xmlrpc
Disable XML-RPC
bool
false
feed_links
Include RSS feed links in page head
bool
true
heartbeat
Modify or disable the WordPress heartbeat. Set to integer to change, false
to disable
bool/int
null
set_login_errors
Hide or change login error messages to mitigate brute force attacks and username phishing
bool/string
null
howdy_message
Change (string) or remove (false
/null) Howdy message in WP admin bar
bool/string/null
true
meta_generator
Enable or change meta generator tags in page head and RSS feeds
bool/string
false
script_attributes
Enable support for additional attributes to script tags via wp_enqueue_script()
bool
flase
shortcodes
Enable custom shortcodes created by this class
bool
false
windows_live_writer
Enable Windows Live Writer support
bool
true
Sample
Via Configuration File (PHP 5.6 or higher)
This is the preferred method if you wish to avoid having a complex array in your wp-config.php
:
// Load configuration from a file in webroot. define( 'CTK_CONFIG', 'sample-config.json' ); // Load configuration from a file off of the parent directory of webroot define( 'CTK_CONFIG', '../conf/sample-config.json' );
See sample-config.json for example.
Via Array (PHP 7 or higher)
Rather than using a JSON file for configuration, you can set CTK_CONFIG
to an array of valyes in wp-config.php
:
define( 'CTK_CONFIG', [ 'disable_emojis' => true, 'admin_bar_color' => '#336699', 'script_attributes' => true, 'meta_generator' => 'Atari 2600' ] );
Caching JSON Config File
If your WordPress Instance has caching enabled, you can configure this plugin to cache the contents of your configuration JSON file with a constant in wp-config.php
:
define( 'CTK_CACHE_EXPIRE', 120 ); // In seconds
Getting Configuration Values
You can use the ctk_config
filter to retrieve values from the config registry (including custom). Using sample-config.json as an example:
// Get meta generator value $meta_generator = apply_filter( 'ctk_config', 'common_toolkit/meta_generator' ); // Get single custom variable $ny_var = apply_filter( 'ctk_config', 'my_custom_variable' ); // Get an array of classic books $classic_books = apply_filter( 'ctk_config', 'nested_example/books/classics' ); // Get entire config registry as associative array $config = apply_filter( 'ctk_config', null );
You can add any variable you want to make available to your site’s themes and plugins.
Usage Examples
WordPress Environment
You can set your instance environment using the following methods (in order of precedence; defaults to “production” if not set using any of the following methods):
wp-config.php
1. Define a Constant in define( 'WP_ENV', 'staging' );
If you wish to use a different constant name, you can set the environment_constant
in the config:
define( 'MY_ENVIRONMENT', 'development' ); define( 'CTK_CONFIG', [ 'environment_constant' => 'MY_ENVIRONMENT' ] );
This will also change the name of the environmental variable used to retrieve the environment:
echo getenv( 'MY_ENVIRONMENT' ); // Result: development // ...or: echo apply_filters( 'ctk_environment', null ); // Result: development
environment
Variable in Config
2. Define Setting:
define( 'CTK_CONFIG', [ 'environment' => 'staging' ] );
Getting:
echo getenv( 'WP_ENV' ); // Result: staging // ...or: echo apply_filters( 'ctk_environment', null ); // Result: staging
If not defined, “production” is returned.
Add Attributes to Enqueued Scripts
Examples:
wp_enqueue_script( 'script-async-with-preload-example', get_template_directory_uri() ....