Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

BB-Code-like mechanism by which strings enclosed in square brackets can serve as shorthand for longer and more complicated strings. These strings are processed by callback handlers, usually on page load, which replace the bracket enclosed string with the output from the callback handler.

A shortcode is a string in square brackets that will be replaced by a callback function.

There are seven built-in shortcodes in WordPress: [audio], [video], [playlist], [embed], [wp_caption], [caption] and [gallery].

A shortcode can be registered with:

add_shortcode( 'shortcode_name', 'callback_function' );

The callback must return a string, it must not output anything directly.


function shortcode_callback()
    echo 'called';

Hello, I am doing it wrong.


function shortcode_callback()
    return 'called';

The callback function can use three parameters.


A shortcode was used in a post like this:

[sample color="red" tag="div"]Some text.[/sample]

The callback can use the information from this shortcode like this

function shortcode_callback( $args, $content = '', $shortcode_name = '' )
    $output = '<' . $args['tag'] . ' style="color:' . $args['color'] . '">' 
        . 'The shortcode ' . $shortcode_name . ' was called.<br>'
        . $content
        . '</' . $args['tag'] . '>';

    return $output;

See also the Codex: Shortcode API.

history | excerpt history