2008-01-25 14:21:11 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
An API for creating shortcode tags that support attributes and enclosed content, such as:
|
|
|
|
|
|
|
|
[shortcode /]
|
|
|
|
[shortcode foo="bar" baz="bing" /]
|
|
|
|
[shortcode foo="bar"]content[/shortcode]
|
|
|
|
|
|
|
|
tag and attrbute parsing regexp code based on the Textpattern tag parser.
|
|
|
|
|
|
|
|
To apply shortcode tags to content:
|
|
|
|
|
|
|
|
$out = do_shortcode($content);
|
|
|
|
|
|
|
|
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));
|
2008-02-05 01:47:27 -05:00
|
|
|
|
2008-01-25 14:21:11 -05:00
|
|
|
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');
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
$shortcode_tags = array();
|
|
|
|
|
2008-04-16 17:07:04 -04:00
|
|
|
function add_shortcode($tag, $func, $after_formatting = false) {
|
2008-01-25 14:21:11 -05:00
|
|
|
global $shortcode_tags;
|
|
|
|
|
2008-04-16 17:07:04 -04:00
|
|
|
if ( is_callable($func) ) {
|
|
|
|
$shortcode_tags[($after_formatting)? 11:9][$tag] = $func;
|
|
|
|
}
|
2008-01-25 14:21:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function remove_shortcode($tag) {
|
|
|
|
global $shortcode_tags;
|
|
|
|
|
2008-04-16 17:07:04 -04:00
|
|
|
unset($shortcode_tags[9][$tag], $shortcode_tags[11][$tag]);
|
2008-01-25 14:21:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function remove_all_shortcodes() {
|
|
|
|
global $shortcode_tags;
|
|
|
|
|
|
|
|
$shortcode_tags = array();
|
|
|
|
}
|
|
|
|
|
2008-04-16 17:07:04 -04:00
|
|
|
function do_shortcode_after_formatting($content) {
|
|
|
|
return do_shortcode($content, true);
|
|
|
|
}
|
|
|
|
function do_shortcode($content, $after_formatting = false) {
|
|
|
|
$pattern = get_shortcode_regex($after_formatting);
|
|
|
|
if (!$pattern) {
|
|
|
|
return $content;
|
|
|
|
} else {
|
|
|
|
$callback_func = 'do_shortcode_tag';
|
|
|
|
if ($after_formatting)
|
|
|
|
$callback_func .= '_after_formatting';
|
|
|
|
|
|
|
|
return preg_replace_callback('/' . $pattern . '/s', $callback_func, $content);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
function get_shortcode_regex($after_formatting) {
|
2008-01-25 14:21:11 -05:00
|
|
|
global $shortcode_tags;
|
|
|
|
|
2008-04-16 17:07:04 -04:00
|
|
|
if (empty($shortcode_tags[($after_formatting)? 11:9]) || !is_array($shortcode_tags[($after_formatting)? 11:9]))
|
|
|
|
return false;
|
2008-01-25 14:21:11 -05:00
|
|
|
|
2008-04-16 17:07:04 -04:00
|
|
|
$tagnames = array_keys($shortcode_tags[($after_formatting)? 11:9]);
|
2008-01-25 14:21:11 -05:00
|
|
|
$tagregexp = join( '|', array_map('preg_quote', $tagnames) );
|
|
|
|
|
2008-04-16 17:07:04 -04:00
|
|
|
return '\[('.$tagregexp.')\b(.*?)(?:(\/))?\](?:(.+?)\[\/\1\])?';
|
2008-01-25 14:21:11 -05:00
|
|
|
}
|
|
|
|
|
2008-04-16 17:07:04 -04:00
|
|
|
function do_shortcode_tag_after_formatting($m) {
|
|
|
|
return do_shortcode_tag($m, true);
|
|
|
|
}
|
|
|
|
function do_shortcode_tag($m, $after_formatting = false) {
|
2008-01-25 14:21:11 -05:00
|
|
|
global $shortcode_tags;
|
|
|
|
|
|
|
|
$tag = $m[1];
|
|
|
|
$attr = shortcode_parse_atts($m[2]);
|
|
|
|
|
|
|
|
if ( isset($m[4]) ) {
|
|
|
|
// enclosing tag - extra parameter
|
2008-04-16 17:07:04 -04:00
|
|
|
return call_user_func($shortcode_tags[($after_formatting)? 11:9][$tag], $attr, $m[4]);
|
2008-01-25 14:21:11 -05:00
|
|
|
} else {
|
|
|
|
// self-closing tag
|
2008-04-16 17:07:04 -04:00
|
|
|
return call_user_func($shortcode_tags[($after_formatting)? 11:9][$tag], $attr);
|
2008-01-25 14:21:11 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function shortcode_parse_atts($text) {
|
|
|
|
$atts = array();
|
2008-02-19 01:52:30 -05:00
|
|
|
$pattern = '/(\w+)\s*=\s*"([^"]*)"(?:\s|$)|(\w+)\s*=\s*\'([^\']*)\'(?:\s|$)|(\w+)\s*=\s*([^\s\'"]+)(?:\s|$)|"([^"]*)"(?:\s|$)|(\S+)(?:\s|$)/';
|
2008-01-25 14:21:11 -05:00
|
|
|
if ( preg_match_all($pattern, $text, $match, PREG_SET_ORDER) ) {
|
|
|
|
foreach ($match as $m) {
|
|
|
|
if (!empty($m[1]))
|
|
|
|
$atts[strtolower($m[1])] = stripcslashes($m[2]);
|
|
|
|
elseif (!empty($m[3]))
|
|
|
|
$atts[strtolower($m[3])] = stripcslashes($m[4]);
|
|
|
|
elseif (!empty($m[5]))
|
|
|
|
$atts[strtolower($m[5])] = stripcslashes($m[6]);
|
2008-02-19 01:52:30 -05:00
|
|
|
elseif (isset($m[7]) and strlen($m[7]))
|
|
|
|
$atts[] = stripcslashes($m[7]);
|
|
|
|
elseif (isset($m[8]))
|
|
|
|
$atts[] = stripcslashes($m[8]);
|
2008-01-25 14:21:11 -05:00
|
|
|
}
|
2008-02-20 11:47:50 -05:00
|
|
|
} else {
|
|
|
|
$atts = ltrim($text);
|
2008-01-25 14:21:11 -05:00
|
|
|
}
|
|
|
|
return $atts;
|
|
|
|
}
|
|
|
|
|
|
|
|
function shortcode_atts($pairs, $atts) {
|
2008-03-23 22:57:19 -04:00
|
|
|
$atts = (array)$atts;
|
2008-01-25 14:21:11 -05:00
|
|
|
$out = array();
|
|
|
|
foreach($pairs as $name => $default) {
|
|
|
|
if ( array_key_exists($name, $atts) )
|
|
|
|
$out[$name] = $atts[$name];
|
|
|
|
else
|
|
|
|
$out[$name] = $default;
|
|
|
|
}
|
|
|
|
return $out;
|
|
|
|
}
|
|
|
|
|
2008-04-16 17:07:04 -04:00
|
|
|
add_filter( 'the_content', 'do_shortcode', 9 );
|
|
|
|
add_filter( 'the_content', 'do_shortcode_after_formatting', 11 );
|
2008-01-25 14:21:11 -05:00
|
|
|
|
|
|
|
?>
|