PHPackages                             brightnucleus/shortcodes - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. brightnucleus/shortcodes

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

brightnucleus/shortcodes
========================

Config-driven WordPress shortcodes.

v0.5.0(10mo ago)112.5k2[2 issues](https://github.com/brightnucleus/shortcodes/issues)[1 PRs](https://github.com/brightnucleus/shortcodes/pulls)MITPHPPHP &gt;=8.0CI failing

Since Feb 17Pushed 10mo ago2 watchersCompare

[ Source](https://github.com/brightnucleus/shortcodes)[ Packagist](https://packagist.org/packages/brightnucleus/shortcodes)[ RSS](/packages/brightnucleus-shortcodes/feed)WikiDiscussions master Synced today

READMEChangelog (2)Dependencies (14)Versions (35)Used By (0)

Bright Nucleus Shortcodes Component
===================================

[](#bright-nucleus-shortcodes-component)

### Config-driven WordPress shortcodes.

[](#config-driven-wordpress-shortcodes)

[![Scrutinizer Code Quality](https://camo.githubusercontent.com/844ffe00551838bb6d72a77e28e57f2904875b72cd65f551ef31a0da0b0df347/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6272696768746e75636c6575732f73686f7274636f6465732f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/brightnucleus/shortcodes/?branch=master)[![Code Coverage](https://camo.githubusercontent.com/f6d7f3e6354b1d6a0c500f6ad4c5b0ae7e2803083097511de356033c7491d385/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6272696768746e75636c6575732f73686f7274636f6465732f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/brightnucleus/shortcodes/?branch=master)[![Build Status](https://camo.githubusercontent.com/da2c251583717ca4ae100b5dadac6d9556bb72247456fe5d3523bd13db0509ff/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6272696768746e75636c6575732f73686f7274636f6465732f6261646765732f6275696c642e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/brightnucleus/shortcodes/build-status/master)[![Codacy Badge](https://camo.githubusercontent.com/46ba3e13e89d8c3a5edb1bbfbb08def3613e2f7e5acf4ea0bbac967aca850107/68747470733a2f2f6170692e636f646163792e636f6d2f70726f6a6563742f62616467652f67726164652f3633653962636361363764363438386138663966303732316534633833656533)](https://www.codacy.com/app/BrightNucleus/shortcodes)[![Code Climate](https://camo.githubusercontent.com/944144e19cebbe8ca467ec1ae0e3cde67418e775eaec7ae3d780fda77dfc8ae5/68747470733a2f2f636f6465636c696d6174652e636f6d2f6769746875622f6272696768746e75636c6575732f73686f7274636f6465732f6261646765732f6770612e737667)](https://codeclimate.com/github/brightnucleus/shortcodes)

[![Latest Stable Version](https://camo.githubusercontent.com/98778ba880d213979968787c18fb975fd251c271fc21b8feeb5aa2902acc8abe/68747470733a2f2f706f7365722e707567782e6f72672f6272696768746e75636c6575732f73686f7274636f6465732f762f737461626c65)](https://packagist.org/packages/brightnucleus/shortcodes)[![Total Downloads](https://camo.githubusercontent.com/e740ba473f287f3b8d9a31bff90235a13bbbc88f5b278b0b45649e0be500ab3f/68747470733a2f2f706f7365722e707567782e6f72672f6272696768746e75636c6575732f73686f7274636f6465732f646f776e6c6f616473)](https://packagist.org/packages/brightnucleus/shortcodes)[![Latest Unstable Version](https://camo.githubusercontent.com/e187dcfee4640188681b7156abc21c8e54f2bb4ed1c3d2f1adc0430e32716001/68747470733a2f2f706f7365722e707567782e6f72672f6272696768746e75636c6575732f73686f7274636f6465732f762f756e737461626c65)](https://packagist.org/packages/brightnucleus/shortcodes)[![License](https://camo.githubusercontent.com/1b4e3f92ae84ac77be093878ed0dbc7d6fb5acd954f84d7c4549f7f631484d8b/68747470733a2f2f706f7365722e707567782e6f72672f6272696768746e75636c6575732f73686f7274636f6465732f6c6963656e7365)](https://packagist.org/packages/brightnucleus/shortcodes)

This is a WordPress shortcodes component that lets you define shortcodes through a config file, complete with dependencies, localization and Shortcake UI.

Table Of Contents
-----------------

[](#table-of-contents)

- [Installation](#installation)
- [Basic Usage](#basic-usage)
- [Configuration Schema](#configuration-schema)
- [Registering A Basic Shortcode](#registering-a-basic-shortcode)
    - [Configuration File](#configuration-file)
    - [Template File](#template-file)
    - [Initialization](#initialization)
- [Using Custom Classes](#using-custom-classes)
- [Using Relative Views](#using-relative-views)
- [Adding Additional Context](#adding-additional-context)
- [Using An External DI](#using-an-external-di)
- [Contributing](#contributing)
- [License](#license)

Installation
------------

[](#installation)

The best way to use this component is through Composer:

```
composer require brightnucleus/shortcodes
```

Basic Usage
-----------

[](#basic-usage)

To use this component, you'll need to:

1. instantiate the `ShortcodeManager` class;
2. inject an object implementing the `BrightNucleus\Config\ConfigInterface` through its constructor;
3. call its `register()` method.

```
use BrightNucleus\Config\ConfigFactory;
use BrightNucleus\Shortcode\ShortcodeManager;

$config = ConfigFactory::create( __DIR__ . '/../config/example_config.php');
$shortcode_manager = new ShortcodeManager(
	$config->getSubConfig( 'ShortcodeManager' )
);
$shortcode_manager->register();
```

Configuration Schema
--------------------

[](#configuration-schema)

```
$shortcodes_config = [

	/* For each shortcode you wish to define, you'll need one separate entry at
	 * the root config entry passed in to ShortcodeManager. The name of that
	 * entry is used as the shortcode tag.
	 */
	'shortcode_tag' => [

		/* Path to a template that is used to render the shortcode.
		 * The path is relative to the configuration file.
		 */
		'view' => __DIR__ . '/../views/shortcodes/view_file.php',

		/* Customised ShortcodeInterface implementation. (optional)
		 * You can use this to completely customize the standard shortcode
		 * class behavior.
		 * Omit to use default `Shortcode` class.
		 * This can be either a fully qualified class name or a callable.
		 */
		'custom_class' => '\BrightNucleus\Shortcodes\Shortcode',

		/* Customised ShortcodeAttsParserInterface implementation. (optional)
		 * You can use this to completely customize the way shortcode attributes
		 * are parsed.
		 * Omit to use default `ShortcodeAttsParser` class.
		 * This can be either a fully qualified class name or a callable.
		 */
		'custom_atts_parser' => '\BrightNucleus\Shortcodes\ShortcodeAttsParser',

		/* Collection of attributes that can be used with the shortcode.
		 * These attributes will be processed by the
		 * `ShortcodeAttsParserInterface` implementation that is being used.
		 */
		'atts' => [

			/* Shortcode attribute name.
			 * These are the optional attributes that you can append to your
			 * shortcode within the WP editor: [shortcode att_name='value'].
			 * NB: lower-cased by WP, so no camelCase or UPPER_CASE.
			 */
			'attribute_name'    => [

				/* Provided that you use the default `ShortcodeAttsParser`
				 * implementation, you can define a `default` value for each
				 * attribute, as well as an optional `validate` callable that
				 * gets evaluated to a boolean.
				 */
				'default'  => 'default_value',
				'validate' => function ( $att ) {
					return some_validation_function( $att );
				},
			],
		],

		/* Customised ShortcodeUIInterface implementation. (optional)
		 * You can use this to completely customize the standard shortcode
		 * user interface class behavior.
		 * Omit to use default `ShortcodeUI` class.
		 * This can be either a fully qualified class name or a callable.
		 */
		'custom_ui' => '\BrightNucleus\Shortcodes\ShortcodeUI',

		/* Besides one additional keys that ShortcodeManager recognizes, the
		 * 'ui' subkey gets passed as is to the Shortcake UI plugin.
		 * Refer to the Shortcake documentation for details about the syntax:
		 * https://github.com/wp-shortcake/shortcake/wiki/Registering-Shortcode-UI
		 */
		'ui'   => [

			/* Whether the shortcode UI (along with its dependencies) is needed
			 * within the current context or not. If this is a callable, it gets
			 * executed and its result evaluated to boolean.
			 */
			'is_needed' => function ( $context ) {
				return true;
			},

			// [ Shortcake configuration keys. ]

		],
	]
];
```

Registering a basic shortcode
-----------------------------

[](#registering-a-basic-shortcode)

For the following example, we'll register a new shortcode that provides a simple `[button]` shortcode. We want the shortcode to be configurable through Shortcake.

### Configuration File

[](#configuration-file)

First, we need to define the shortcode through a configuration file.

```
