Codex tools: Log in
Contents |
Adds a hook for a shortcode tag.
<?php add_shortcode( $tag , $func ); ?>
Simplest example of a shortcode tag using the API: [footag foo="bar"]
function footag_func( $atts ) {
return "foo = {$atts[foo]}";
}
add_shortcode('footag', 'footag_func');
Example with nice attribute defaults: [bartag foo="bar"]
function bartag_func( $atts ) {
extract( shortcode_atts( array(
'foo' => 'no foo',
'baz' => 'default baz'
), $atts ) );
return "foo = {$foo}";
}
add_shortcode( 'bartag', 'bartag_func' );
Example with enclosed content: [baztag]content[/baztag]
function baztag_func( $atts, $content="" ) {
return "content = $content";
}
add_shortcode( 'baztag', 'baztag_func' );
If your plugin is designed as a class write as follows:
class MyPlugin {
function baztag_func() {
return "content = $content";
}
}
add_shortcode( 'baztag', array( 'MyPlugin', 'baztag_func' ) );
There can only be one hook for each shortcode. Which means that if another plugin has a similar shortcode, it will override yours or yours will override theirs depending on which order the plugins are included and/or ran.
Shortcode attribute names are always converted to lowercase before they are passed into the handler function. Values are untouched.
Note that the function called by the shortcode should never produce output of any kind. Shortcode functions should return the text that is to be used to replace the shortcode. Producing the output directly will lead to unexpected results. This is similar to the way filter functions should behave, in that they should not produce expected side effects from the call, since you cannot control when and where they are called from.
Since: 2.5
add_shortcode() is located in wp-includes/shortcodes.php.
Shortcode: do_shortcode(), add_shortcode(), remove_shortcode(), remove_all_shortcodes(), shortcode_atts(), strip_shortcodes()