Basic Shortcodes

Add a Shortcode Add a Shortcode

It is possible to add your own shortcodes by using the Shortcode API. The process involves registering a callback $func to a shortcode $tag using add_shortcode().

<?php
add_shortcode(
    string $tag,
    callable $func
);
?>

[wporg] is your new shortcode. The use of the shortcode will trigger the wporg_shortcode callback function.

Unlike a Theme, a Plugin is run at a very early stage of the loading process thus requiring us to postpone the adding of our shortcode until WordPress has been initialized.

We recommend the init action hook.

<?php
function wporg_shortcodes_init()
{
    function wporg_shortcode($atts = [], $content = null)
    {
        // do something to $content

        // always return
        return $content;
    }
    add_shortcode('wporg', 'wporg_shortcode');
}
add_action('init', 'wporg_shortcodes_init');
?>

Top ↑

Remove a Shortcode Remove a Shortcode

It is possible to remove shortcodes by using the Shortcode API. The process involves removing a registered $tag using remove_shortcode().




<?php
remove_shortcode(
    string $tag
);
?>

Make sure that the shortcode have been registered before attempting to remove. Specify a higher priority number for add_action() or hook into an action hook that is run later.

Top ↑

Check if a Shortcode Exists Check if a Shortcode Exists

To check whether a shortcode has been registered use shortcode_exists().