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
.
$shortcode->add('UserProGetUsername', 'getUsername');
$shortcode->add(
'MyPluginShowPostName',
'myPluginApi::showPostName'
);
This would then be accessed by the shortcode like so:
[MyPluginShowPostName]
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);
});
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'
]
);