Adds a new shortcode.
Description
Care should be taken through prefixing or other means to ensure that the shortcode tag being added is unique and will not conflict with other, already-added shortcode tags. In the event of a duplicated tag, the tag loaded last will take precedence.
Parameters
$tag
stringrequired- Shortcode tag to be searched in post content.
$callback
callablerequired- The callback function to run when the shortcode is found.
Every shortcode callback is passed three parameters by default, including an array of attributes ($atts
), the shortcode content or null if not set ($content
), and finally the shortcode tag itself ($shortcode_tag
), in that order.
Source
function add_shortcode( $tag, $callback ) {
global $shortcode_tags;
if ( '' === trim( $tag ) ) {
_doing_it_wrong(
__FUNCTION__,
__( 'Invalid shortcode name: Empty name given.' ),
'4.4.0'
);
return;
}
if ( 0 !== preg_match( '@[<>&/\[\]\x00-\x20=]@', $tag ) ) {
_doing_it_wrong(
__FUNCTION__,
sprintf(
/* translators: 1: Shortcode name, 2: Space-separated list of reserved characters. */
__( 'Invalid shortcode name: %1$s. Do not use spaces or reserved characters: %2$s' ),
$tag,
'& / < > [ ] ='
),
'4.4.0'
);
return;
}
$shortcode_tags[ $tag ] = $callback;
}
Changelog
Version | Description |
---|---|
2.5.0 | Introduced. |
Examples
Simplest example of a shortcode tag using the API:
[footag foo="bar"]
Example with nice attribute defaults:
[bartag foo="bar"]
Example with enclosed content:
[baztag]content[/baztag]
If your plugin is designed as a class write as follows:
When adding `add_shortcode() ` function in a plugin, it’s good to add it in a function that is hooked to `init` hook. So that WordPress has time to initialize properly.
As described in the plugins handbook.
Example of
add_shortcode
function that usesget_template_part
function to include a template.For wppb plugin boilerplate I add code to define_public_hooks()
$this->loader->add_action( 'init', $plugin_public, 'register_shortcodes' );
Then in public folder, in class_public file I added:
Beware that the first argument passed to the callback is an empty string if no attributes are set in the shortcode. So a type declaration will fail:
“Fatal error: Uncaught Error: Argument 1 passed to shortcode_callback() must be of the type array, string given”
Use a check whether
$arguments
is not an array and if so, replace it with an empty array:shortcode_atts()
this will already be taken care of. If using PHP 8.0+, you can also declare the parameter asarray|string
.function shortcode_callback( array|string $atts, string $content, string $shortcode_tag ) { $atts = shortcode_atts( array( 'attribute' => 'default_value' ), $atts ); // do stuff }
shortcode_atts()
this will already be taken care of. If using PHP 8.0+, you can also declare the parameter asarray|string
.Notes (from the Codex — https://codex.wordpress.org/Function_Reference/add_shortcode#Notes)
⚠️⚠️
Currently, and for some time now, viewing a post with a shortcode in the editor caused the shortcode to get rendered. This is very unexpected behaviour described in an old issue. It can, for example, cause the post to become uneditable via the admin if there’s an error in the shortcode, such as some visitor-facing area variables or functions not being declared, etc.
To go around this, as mentioned in a comment, use the
is_admin()
function to prevent computation on admin pages.echo
instead ofreturn
in the callback. Maybe this was fixed in the past few months… or you were usingecho
like me. Also, if you useecho
the shortcode content will not appear where the shortcode was placed. So it is best toreturn
the output of the callback function.$atts: an associative array of attributes, you can access the associative array anywhere.
$content: the enclosed content (if the shortcode is used in its enclosing form)
When passing values via the shortcode e.g.
[myshortcode FooBar="one" SomethingElse="two"]
the keys in the array are transformed to lowercaseThe array passed to the callback function will be:
Array( foobar => "one", somethingelse=> "two" )
Don’t be like me and spend hours trying to figure out why the values weren’t being passed.