Skip to Content
✨ WP Bones 1.9.6 is out! Check the Release Notes →

Shortcodes

This document explains how to create and manage shortcodes in WP Bones. It provides steps to create a Shortcode Service Provider using the php bones make:shortcode MyShortcodes command and how to manually create one. The new provider will be created in the plugin/Shortcodes directory. The document also covers defining shortcodes and their methods, and how to load the Shortcode Service Provider by adding it to the list of providers in the config/plugin.php file.

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 plugin/Shortcodes directory. Of course, you may create your Service Provider manually and in any directory you prefer. You have to change the namespace accordingly.

plugin/Shortcodes/MyShortcodes.php
<?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:

config/plugin.php
/* |-------------------------------------------------------------------------- | 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.

Last updated on