A shortcode is a string in square brackets that will be replaced by a callback function.
There are four built-in shortcodes in WordPress: [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.
Wrong:
function shortcode_callback()
{
echo 'called';
?>
Hello, I am doing it wrong.
<?php
}
Correct:
function shortcode_callback()
{
return 'called';
}
The callback function can use three parameters.
Example
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.