add_shortcode( string $tag, callable $callback )
Adds a new shortcode.
Description 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 Parameters
- $tag
-
(string) (Required) Shortcode tag to be searched in post content.
- $callback
-
(callable) (Required) 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 Source
File: wp-includes/shortcodes.php
function add_shortcode( $tag, $callback ) {
global $shortcode_tags;
if ( '' == trim( $tag ) ) {
$message = __( 'Invalid shortcode name: Empty name given.' );
_doing_it_wrong( __FUNCTION__, $message, '4.4.0' );
return;
}
if ( 0 !== preg_match( '@[<>&/\[\]\x00-\x20=]@', $tag ) ) {
/* translators: 1: Shortcode name, 2: Space-separated list of reserved characters. */
$message = sprintf( __( 'Invalid shortcode name: %1$s. Do not use spaces or reserved characters: %2$s' ), $tag, '& / < > [ ] =' );
_doing_it_wrong( __FUNCTION__, $message, '4.4.0' );
return;
}
$shortcode_tags[ $tag ] = $callback;
}
Expand full source code Collapse full source code View on Trac
Changelog Changelog
| Version | Description |
|---|---|
| 2.5.0 | Introduced. |
User Contributed Notes User Contributed Notes
You must log in before being able to contribute a note or feedback.
Examples
Simplest example of a shortcode tag using the API:
[footag foo="bar"]add_shortcode( 'footag', 'wpdocs_footag_func' ); function wpdocs_footag_func( $atts ) { return "foo = {$atts['foo']}"; }Example with nice attribute defaults:
[bartag foo="bar"]add_shortcode( 'bartag', 'wpdocs_bartag_func' ); function wpdocs_bartag_func( $atts ) { $atts = shortcode_atts( array( 'foo' => 'no foo', 'baz' => 'default baz' ), $atts, 'bartag' ); return "foo = {$atts['foo']}"; }Example with enclosed content:
[baztag]content[/baztag]add_shortcode( 'baztag', 'wpdocs_baztag_func' ); function wpdocs_baztag_func( $atts, $content = "" ) { return "content = $content"; }If your plugin is designed as a class write as follows:
add_shortcode( 'baztag', array( 'MyPlugin', 'wpdocs_baztag_func' ) ); class MyPlugin { public static function wpdocs_baztag_func( $atts, $content = "" ) { return "content = $content"; } }Notes (from the Codex — https://codex.wordpress.org/Function_Reference/add_shortcode#Notes)
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.
add_action( 'init', 'wpdocs_add_custom_shortcode' ); function wpdocs_add_custom_shortcode() { add_shortcode( 'footag', 'wpdocs_footag_func' ); }As described in the plugins handbook.