Shortcodes
Overview
WordPress provides a powerful way to create your own shortcodes. WP Bones provides a simple way to add your own Shortcodes.
Create a Shortcode Service Provider
You may create your own shortcode service provider by following the steps below:
php bones make:shortcode MyShortcodes
By default, the new provider will be created in the plugins/Shortcodes
directory. Of course, you may create your Service Provider manually and in any directory you prefer. You have to change the namespace accordingly.
<?php
namespace WPKirk\Shortcodes;
use WPKirk\WPBones\Foundation\WordPressShortcodesServiceProvider;
class MyShortcodes extends WordPressShortcodesServiceProvider {
/**
* List of registred shortcodes. {shortcode}/method
*
* @var array
*/
protected $shortcodes = [
'my-shortcode' => 'myShortcodeMethod'
];
/**
* Example of shortcode.
*
* @param array $atts Optional.Attribute into the shortcode
* @param null $content Optional. HTML content
*
* @return string
*/
public function myShortcodeMethod( $atts = [ ], $content = null )
{
// Default values for shortcode
$defaults = array(
'computer' => false,
);
$atts = shortcode_atts( $defaults, $atts, 'wp_kirk' );
return 'Computer, engage';
}
}
Load the Shortcode Service Provider
Add this new Service Provider to the list of providers in the /config/plugin.php
file:
/*
|--------------------------------------------------------------------------
| Shortcodes
|--------------------------------------------------------------------------
|
| Here is where you can register the Shortcodes.
|
*/
'shortcodes' => [ '\WPKirk\Shortcodes\MyShortcodes' ],
Of course, you can add one or more classes in the shortcodes
array.