Disable automatic formatting inside WordPress shor

2019-01-31 22:05发布

问题:

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 1 shortcode at a time... AND b/c 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?

回答1:

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.



回答2:

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 Formatting - 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.



回答3:

In my case - this solution has broken one of the side shortcodes (revslider). So I've found another solution here: http://wordpress-hackers.1065353.n5.nabble.com/shortcode-unautop-tp42085p42086.html which is to use another filter like this:

// via http://www.wpexplorer.com/clean-up-wordpress-shortcode-formatting/
if ( !function_exists('wpex_fix_shortcodes') ) {
    function wpex_fix_shortcodes($content){
        $array = array (
            '<p>[' => '[',
            ']</p>' => ']',
            ']<br />' => ']'
        );
        $content = strtr($content, $array);
        return $content;
    }
    add_filter('the_content', 'wpex_fix_shortcodes');
}

Works fine for me :)



回答4:

Please see my answer here: https://wordpress.stackexchange.com/questions/55782/remove-wpautop-from-shortcode-content-remove-whitespace-in-buffering/211784#answer-211784

It allows you to turn off wpautop for as many specific shortcodes as you want using this:

include "shortcode-wpautop-control.php";
chiedolabs_shortcode_wpautop_control(array('shortcodeone', 'shortcodetwo'));


标签: wordpress