Skip to content

Latest commit

 

History

History
77 lines (56 loc) · 1.58 KB

shortcodes.md

File metadata and controls

77 lines (56 loc) · 1.58 KB

Shortcodes

Shortcodes are for non-technical users to access some function of your plugin within the WordPress content editor. Therefore, adding a shortcode is generally just a reference to an API method. Shortcodes consist of a Name, API Call and Argument Name Array (more on this later). They are defined in app/shortcodes.php.

Adding a Basic Shortcode

$shortcode->add('UserProGetUsername', 'getUsername');
$shortcode->add(
    'MyPluginShowPostName',
    'myPluginApi::showPostName'
);

This would then be accessed by the shortcode like so:

[MyPluginShowPostName]

Handling Argument Names

Unfortunately WordPress doesn't support camel case in shortcode arguments; You'll need to supply an array to rename them.

$shortcode->add(
    'MyPluginShowPostName',
    'myPluginApi::showPostName',
    [
        'post_id' => 'postId'
    ]
);

This would then be accessed by the shortcode like so:

[MyPluginShowPostName post_id=2]

With your API method:

$api->add('showPostName', function($postId)
{
    return (new PostController)->showPostName($postId);
});

Using any controller method or closure

In some cases you may need to a call normal controller method or do some setup before hand in a closure.

$shortcode->add(
    'MyPluginShowPostName',
    __NAMESPACE__ . '\Controllers\PostController@showPostName',
    [
        'post_id' => 'postId'
    ]
);

Or using a closure

$shortcode->add(
    'MyPluginShowPostName',
    function($postId)
    {
        //Some awesome code
    },
    [
        'post_id' => 'postId'
    ]
);