Question

Disable automatic formatting inside WordPress shortcodes

I've seen the tutorials on creating a (raw) shortcode that leaves the code inside it untouched,

http://www.wprecipes.com/disable-wordpress-automatic-formatting-on-posts-using-a-shortcode

but unfortunately this only applies to one shortcode at a time... and because the else statement bypasses the normal filters and calls the functions directions. My other modifications to autop and texturize functions get ignored.

Is there a way to 1. match multiple shortcodes and 2. preserve my other add/remove filters to the_content?

 21  21045  21
1 Jan 1970

Solution

 47

After implementing @helgatheviking's solution on multiple websites, I'm convinced that only these lines are required:

// Move wpautop filter to AFTER shortcode is processed
remove_filter('the_content', 'wpautop');
add_filter('the_content', 'wpautop', 99);
add_filter('the_content', 'shortcode_unautop', 100);

Put them in your functions.php file and you're set.

2013-02-04

Solution

 9

I solved this as best as possible by combining a slightly modified parse_shortcode_content function from Donal MacArthur (his originally manually calls wpautop... which I've removed. With the re-ordering of default filters to run wpautop much later... after the shortcode has already been processed instead of before.

// Clean up WordPress shortcode sormatting - important for nested shortcodes
// Adjusted from http://donalmacarthur.com/articles/cleaning-up-wordpress-shortcode-formatting/
function parse_shortcode_content( $content ) {

   /* Parse nested shortcodes and add formatting. */
    $content = trim( do_shortcode( shortcode_unautop( $content ) ) );

    /* Remove '' from the start of the string. */
    if ( substr( $content, 0, 4 ) == '' )
        $content = substr( $content, 4 );

    /* Remove '' from the end of the string. */
    if ( substr( $content, -3, 3 ) == '' )
        $content = substr( $content, 0, -3 );

    /* Remove any instances of ''. */
    $content = str_replace( array( '<p></p>' ), '', $content );
    $content = str_replace( array( '<p>  </p>' ), '', $content );

    return $content;
}

and moving the filters

// Move wpautop filter to AFTER shortcode is processed
remove_filter( 'the_content', 'wpautop' );
add_filter( 'the_content', 'wpautop', 99);
add_filter( 'the_content', 'shortcode_unautop', 100 );

EDIT:

The parse_shortcode_content() function is no longer required (if it ever was). Simply adjust the filter order.

2011-07-26