Shortcodes
A very powerful feature of WordPress are the shortcodes. You may add your own shortcodes in a very easy way. First of all, similarly to the Ajax, we will need to add the list of your shortcode classes 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.
Typical Shortcode
Let's see a simple implementation of MyShortcodes
class:
<?php filename="/plugin/Shortcodes/MyShortcodes.php" copy
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';
}
}
PHP Bones Command
Using the php bones make:shortcode
Bones command, we can quickly create a Shortcode Service Provider.
The MyShortcodes
class extends WP Bones WordPressShortcodesServiceProvider
class that provides the base implementation for WordPress shortcodes. As you can see, it is very easy to add your own shortcodes.