Codex

Function Reference/add shortcode

Contents

Description

Adds a hook for a shortcode tag.

Usage

<?php add_shortcode$tag $func ); ?>

Parameters

$tag
(string) (required) Shortcode tag to be searched in post content
Default: None
$func
(callable) (required) Hook to run when shortcode is found
Default: None

Return Values

(none)

Examples

Simplest example of a shortcode tag using the API: [footag foo="bar"]

function footag_func($atts) {
     return "foo = {$atts[foo]}";
}

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');


Notes

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.

Change Log

Since: 2.5

Source File

add_shortcode() is located in wp-includes/shortcodes.php.

Related

Shortcode: do_shortcode, add_shortcode, remove_shortcode, remove_all_shortcodes, shortcode_atts, strip_shortcodes

See also index of Function Reference and index of Template Tags.