mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-03-24 07:14:28 +00:00
Allow shortcodes to run before or after wpautop()/texturize() formatting. Default to before for WP 2.5 compat. Props AaronCampbell
git-svn-id: https://develop.svn.wordpress.org/trunk@7699 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -47,17 +47,18 @@ add_shortcode('baztag', 'baztag_func');
|
||||
|
||||
$shortcode_tags = array();
|
||||
|
||||
function add_shortcode($tag, $func) {
|
||||
function add_shortcode($tag, $func, $after_formatting = false) {
|
||||
global $shortcode_tags;
|
||||
|
||||
if ( is_callable($func) )
|
||||
$shortcode_tags[$tag] = $func;
|
||||
if ( is_callable($func) ) {
|
||||
$shortcode_tags[($after_formatting)? 11:9][$tag] = $func;
|
||||
}
|
||||
}
|
||||
|
||||
function remove_shortcode($tag) {
|
||||
global $shortcode_tags;
|
||||
|
||||
unset($shortcode_tags[$tag]);
|
||||
unset($shortcode_tags[9][$tag], $shortcode_tags[11][$tag]);
|
||||
}
|
||||
|
||||
function remove_all_shortcodes() {
|
||||
@@ -66,21 +67,37 @@ function remove_all_shortcodes() {
|
||||
$shortcode_tags = array();
|
||||
}
|
||||
|
||||
function do_shortcode($content) {
|
||||
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) {
|
||||
global $shortcode_tags;
|
||||
|
||||
if (empty($shortcode_tags) || !is_array($shortcode_tags))
|
||||
return $content;
|
||||
if (empty($shortcode_tags[($after_formatting)? 11:9]) || !is_array($shortcode_tags[($after_formatting)? 11:9]))
|
||||
return false;
|
||||
|
||||
$tagnames = array_keys($shortcode_tags);
|
||||
$tagnames = array_keys($shortcode_tags[($after_formatting)? 11:9]);
|
||||
$tagregexp = join( '|', array_map('preg_quote', $tagnames) );
|
||||
|
||||
$pattern = '/\[('.$tagregexp.')\b(.*?)(?:(\/))?\](?:(.+?)\[\/\1\])?/s';
|
||||
|
||||
return preg_replace_callback($pattern, 'do_shortcode_tag', $content);
|
||||
return '\[('.$tagregexp.')\b(.*?)(?:(\/))?\](?:(.+?)\[\/\1\])?';
|
||||
}
|
||||
|
||||
function do_shortcode_tag($m) {
|
||||
function do_shortcode_tag_after_formatting($m) {
|
||||
return do_shortcode_tag($m, true);
|
||||
}
|
||||
function do_shortcode_tag($m, $after_formatting = false) {
|
||||
global $shortcode_tags;
|
||||
|
||||
$tag = $m[1];
|
||||
@@ -88,10 +105,10 @@ function do_shortcode_tag($m) {
|
||||
|
||||
if ( isset($m[4]) ) {
|
||||
// enclosing tag - extra parameter
|
||||
return call_user_func($shortcode_tags[$tag], $attr, $m[4]);
|
||||
return call_user_func($shortcode_tags[($after_formatting)? 11:9][$tag], $attr, $m[4]);
|
||||
} else {
|
||||
// self-closing tag
|
||||
return call_user_func($shortcode_tags[$tag], $attr);
|
||||
return call_user_func($shortcode_tags[($after_formatting)? 11:9][$tag], $attr);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -129,6 +146,7 @@ function shortcode_atts($pairs, $atts) {
|
||||
return $out;
|
||||
}
|
||||
|
||||
add_filter('the_content', 'do_shortcode', 11); // AFTER wpautop()
|
||||
add_filter( 'the_content', 'do_shortcode', 9 );
|
||||
add_filter( 'the_content', 'do_shortcode_after_formatting', 11 );
|
||||
|
||||
?>
|
||||
|
||||
Reference in New Issue
Block a user