Skip to content

Instantly share code, notes, and snippets.

@nextend
Created May 10, 2016 13:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nextend/05d839539f8abb5c1060ccc90a6f6f99 to your computer and use it in GitHub Desktop.
Save nextend/05d839539f8abb5c1060ccc90a6f6f99 to your computer and use it in GitHub Desktop.
WordPress multiline shortcode with wpautop support
class NextendMultiLineShortcode{
static $multiline_shortcode_storage = array();
static $multiline_shortcodes = array();
public static function init(){
add_filter('the_content', 'NextendMultiLineShortcode::do_multiline_shortcode', 9); // before wpautop()
add_shortcode('multiline_shortcode', 'NextendMultiLineShortcode::multiline_shortcode');
}
public static function do_multiline_shortcode($content, $ignore_html = false) {
$pattern = get_shortcode_regex(self::$multiline_shortcodes);
return preg_replace_callback("/$pattern/", 'NextendMultiLineShortcode::replace_multiline_shortcode', $content);
}
public static function replace_multiline_shortcode($m) {
static $counter = 0;
$counter++;
self::$multiline_shortcode_storage[$counter] = $m[0];
return '[multiline_shortcode id=' . $counter . ']';
}
public static function multiline_shortcode($atts) {
return do_shortcode(self::$multiline_shortcode_storage[$atts['id']]);
}
}
NextendMultiLineShortcode::$multiline_shortcodes = array(
'pro',
'free',
'note',
'do',
'dont'
);
NextendMultiLineShortcode::init();
@nextend
Copy link
Author

nextend commented May 10, 2016

Example usage:

[pro]
<ul>
    <li>Device specific slider size</li>
    <li>Disable slider on mobile devices</li>
    <li>Disable controls on mobile devices</li>
    <li>Different background image sizes on mobile</li>
    <li>Retina background image support</li>
</ul>
[/pro]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment