An open API service indexing awesome lists of open source software.

https://github.com/dmhendricks/wordpress-mu-common-toolkit

A must use (MU) plugin for WordPress that contains helper functions, snippets and a configuration registry.
https://github.com/dmhendricks/wordpress-mu-common-toolkit

configuration-management emojis enqueue-scripts mu-plugin wordpress wordpress-amp wordpress-development wordpress-mu-plugin

Last synced: 3 months ago
JSON representation

A must use (MU) plugin for WordPress that contains helper functions, snippets and a configuration registry.

Awesome Lists containing this project

README

        

[![Author](https://img.shields.io/badge/author-Daniel%20M.%20Hendricks-lightgrey.svg?colorB=9900cc&style=flat-square)](https://www.danhendricks.com/?utm_source=github.com&utm_medium=campaign&utm_content=button&utm_campaign=dmhendricks%2Fwordpress-mu-common-toolkit)
[![License](https://img.shields.io/github/license/dmhendricks/wordpress-mu-common-toolkit.svg?style=flat-square)](https://github.com/dmhendricks/wordpress-mu-common-toolkit/blob/master/LICENSE)
[![DigitalOcean](https://img.shields.io/badge/hosting-Digital%20Ocean-green.svg?style=flat-square&label=hosting&colorB=0152FF)](https://m.do.co/t/8a88362f5683?utm_source=github.com&utm_medium=campaign&utm_content=referral&utm_campaign=dmhendricks%2Fwordpress-mu-common-toolkit)
[![Twitter](https://img.shields.io/twitter/url/https/github.com/dmhendricks/wordpress-mu-common-toolkit.svg?style=social)](https://twitter.com/danielhendricks)
[![BrowserStack](https://img.shields.io/static/v1?style=social&label=BrowserStack&message=Passed&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABwAAAAcCAMAAABF0y+mAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAyhpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuNi1jMTQ1IDc5LjE2MzQ5OS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v+xR/0eOgC/rIGvM7SvI9qKjAC+6lG5wf+SM/+2mOGnGf8NMgCcvLLEw5OZOv0yMfvARRfD7v+VSf9lZ/9mOqTs9lvAQ/+EK/+VY7Hr/f+cN0+7Lb2FMuqFG7tuOv9jEOCLLP93JKzj+ZDYwwC9s2LLrQB8k/9wEv+4R/9oD5/g1b3iAACtygCJo//w6MnHyPM5M+h3NuVyfcLPZP9MWKOYXMevu/+Xn5HG06nWAwDG8pzn6//lsZ/l85rik/+def/38fNoILPS3/1tF/+3Zf+sROmuICWPq3edprrG0VTW26jn/EhteD7CN9ibbgC8t/96NPmLQIjPY/9CKSQfIP///////+tAimMAAACAdFJOU/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////8AOAVLZwAAAj9JREFUeNpU0vtb0lAYB/CzCTgeDrQpGwyUyzZwwwyBmTRNELwgkYpQZkUXumktyrJ13eFf9z0CZd8ftufZZ9/znPOcFw1HIWezp+u9Xm/9xaw8nASNXsczfNk0c2VJOjGni2fXkRQ1KWeWdxKBSCSS2DuZZslflDepPX76LrS/P/+7343slWfkMRJque+djGXlIcuo301IH+URFjU+Z97ayAAEIfl8NtxN3GUIxWMNS+aTDS/QysHBij8YXOTC3b21N4DkFIrTH95alt/j+nw+z0owmE32A78UdYhS2JZg0aiV9/hc1zc1C7q4hbolgx0ilgd8vuG1HoK5rkxcT/B+kusHDEFGm5ot5b6kvZbnCh9Nua7/R5ILR9piDP3EVxgdowtPPzRR5I7IIpvi7bSeGSPEvzhCBjUp3rjQo5/H6PO8mt8CbCtxuixvPnut695J1f+1wnHhgAG4pmG7vBu6p6ej367O+cALRa7/SQQsahjzJ8kWaPrl+fmy9yIEVgnUBdhQ7BBj+8+uE9KBa7WaTntcf2EgwFHkAlSxtOpkQ61OpxWqUAuXqoKiyIgwUMVHoFtcpcJRclAJG7AqQUN1DlPlt5OO44A4laUFvi4oYgqujLB1qrjxfnv1JmRp54gvgMGowGXL8blDnpYbDfpPg7dpLy6PZkgVjCrW8Cg8rhpggjqZPjUuGoWmDWQ3CwOB9tR/cyuzoqC0B4OBoVASWfn6UJMUo4gAVBQmRf6beGA1xsQVJc7EVDL5dinAAO2UucfjcPEkAAAAAElFTkSuQmCC)](https://browserstack.com/?utm_source=github.com&utm_medium=referral&utm_content=link&utm_campaign=dmhendricks%2Fwordpress-mu-common-toolkit)

# WordPress Common Toolkit MU Plugin

A simple [MU plugin](https://codex.wordpress.org/Must_Use_Plugins) for WordPress that adds functionality that I use on web site projects, including a [configuration registry](#getting-configuration-values).

- [Installation](#installation)
- [Configuration](#configuration)
- [Sample](#sample)
- [Caching JSON Config File](#caching-json-config-file)
- [Usage Examples](#usage-examples)
- [Environment Filter](#environment-filter)
- [Action Hook](#action-hook)
- [Shortcodes](#shortcodes)

## 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](#add-attributes-to-enqueued-scripts) to script tags via wp_enqueue_script() | bool | flase |
| `shortcodes` | Enable custom [shortcodes](#shortcodes) created by this class | bool | false |
| `windows_live_writer` | Enable [Windows Live Writer](https://is.gd/Q6KjEQ) 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`:

```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](https://github.com/dmhendricks/wordpress-mu-common-toolkit/blob/master/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`:

```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`:

```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](https://github.com/dmhendricks/wordpress-mu-common-toolkit/blob/master/sample-config.json) as an example:

```php
// 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):

#### 1. Define a Constant in `wp-config.php`

```php
define( 'WP_ENV', 'staging' );
```

If you wish to use a different constant name, you can set the `environment_constant` in the config:

```php
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:

```php
echo getenv( 'MY_ENVIRONMENT' ); // Result: development
// ...or:
echo apply_filters( 'ctk_environment', null ); // Result: development
```

#### 2. Define `environment` Variable in Config

Setting:

```php
define( 'CTK_CONFIG', [ 'environment' => 'staging' ] );
```

Getting:

```php
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:

```php
wp_enqueue_script( 'script-async-with-preload-example', get_template_directory_uri() . '/assets/js/script.js#async,preload' );
wp_enqueue_script( 'script-defer-example', get_template_directory_uri() . '/assets/js/script.js#defer' );
wp_enqueue_script( 'script-custom-attributes', 'https://cdn.ampproject.org/v0/amp-audio-0.1.js?custom_attribute[]=custom-element|amp-audio#async' );

wp_enqueue_style( 'stylesheet-with-preload', get_template_directory_uri() . '/assets/css/style.css#preload' );
```

Result:

```html

```

### Change Admin Bar Color

Useful for distinguishing browser windows among different environments. Defined in `wp-config.php`:

```php
define( 'CTK_CONFIG', [ 'admin_bar_color' => '#336699' ] );
```

### Build URL Parsed With parsed_url()

This function reverses the result of [`parse_url()`](http://php.net/manual/en/function.parse-url.php):

```php
$parse_uri = parse_url( 'https://example.com/?hello=world#hash );
$parse_uri['fragment'] = 'newhash';
$uri = \MU_Plugins\CommonToolkit::build_url( $parse_uri );
```

### Disable WordPress Core, Plugin and/or Theme Updates

You can disable any of the update notifications or specific. It accepts string, boolean or an array of values. Examples:

```php
// Disable all update notifications
define( 'CTK_CONFIG', [ 'disable_updates' => true ] ); // boolean

// Disable WordPress core and theme updates only (not plugins)
define( 'CTK_CONFIG', [ 'disable_updates' => [ 'core', 'theme' ] ] ); // array

// Disable only plugin updates updates
define( 'CTK_CONFIG', [ 'disable_updates' => 'plugin' ] ); // string
```

## Hide or Change Login Errors

To help prevent user enumeration/phishing for brute for attacks, you can change the WordPress login errors to something more generic by defining `set_login_errors`. This value can be a string or boolean:

| **Value** | **Result** |
|---------------------------|---------------------------------------------------------------------------------------------|
| _null_ | Leaves the default WordPress messages in place |
| `false` | Hide/disables login error messages completely |
| `true` | Changes the login messages to a generic "Login failed." (English only) |
| _string_ | Changes the login message to your own string, particularly useful if your default language is not English. |

:pushpin: You can use `%s` in your string, which will be replaced with the reset password URL. Example:

```php
define( 'CTK_CONFIG', [ 'set_login_errors' => 'ERROR: Invalid credentials. Lost your password?' ] );
```

## Environment Filter

You can alternately retrieve the current environment using the `ctk_environment` filter:

```php
echo apply_filters( 'ctk_environment', null ); // 'production', 'staging', etc
```

You can also pass `is_production` to determine if we're currently in production more. It compares the value of your environment (defined above) with the value of `common_toolkit/environment_production` (which defaults to "production"). In this way, you can set your production label/string value to whatever you like.

Determining if in production mode using **defaults**:

```php
if( apply_filters( 'ctk_environment', 'is_production' ) ) {
// Do something intended only for production
} else {
// Do something else
}
```

As noted above, you can change the string comparison of what is considered production in config. For example, if you wanted to use "live" instead of "production":

```php
define( 'CTK_CONFIG', [ 'environment_production' => 'live' ] );

// Result: true
define( 'WP_ENV', 'live' ); // wp-config.php
var_dump( apply_filters( 'ctk_environment', 'is_production' ) );

// Result: false
define( 'WP_ENV', 'staging' ); // wp-config.php
var_dump( apply_filters( 'ctk_environment', 'is_production' ) );
```

This special filter value is provided solely for convenience. You may, of course, do a manual comparison:

```php
if( getenv( 'WP_ENV' ) == 'production' ) { // Replace variable with value of `environment_constant`, if set
// Do something intended only for production
} else {
// Do something else
}
```

## Action Hook

If you want to perform some logic only if this script is loaded, you can use the `common_toolkit_loaded` action hook (which executes during the [init](https://codex.wordpress.org/Plugin_API/Action_Reference#Actions_Run_During_a_Typical_Request) phase).

```php
add_action( 'common_toolkit_loaded', function() {
// Do something if common toolkit is loaded ...
var_dump( apply_filters( 'ctk_config', null ) );
});
```

## Shortcodes

### `[get_datetime]`

Returns a formatted date in WordPress configured timezone. Defaults to current date/time in MySQL format. Enabling:

```php
define( 'CTK_CONFIG', [ 'shortcodes' => true ] );
```

Usage:

```
Copyright ©[get_datetime format="Y"] Your Company
Current date/time: [get_datetime]
```

See PHP's [`date()`](https://php.net/date) function for formatting options.

[![Analytics](https://ga-beacon.appspot.com/UA-67333102-2/dmhendricks/wordpress-mu-common-toolkit?flat)](https://github.com/igrigorik/ga-beacon/?utm_source=github.com&utm_medium=referral&utm_content=button&utm_campaign=dmhendricks%2Fwordpress-mu-common-toolkit)