禁用WordPress短代码内的自动格式化

hel*_*ing 21 wordpress

我已经看过有关创建[原始]短代码的教程,这些代码使代码内部不受影响,

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

但不幸的是,这一次只适用于1个短代码...并且b/c else语句绕过普通过滤器并调用函数方向,我对autop和texturize函数的其他修改将被忽略.

有没有办法1.匹配多个短代码和2.保留我的其他添加/删除过滤器到the_content?

Mat*_*ord 45

在多个网站上实施@ helgatheviking的解决方案后,我确信只需要这些行:

//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 );
Run Code Online (Sandbox Code Playgroud)

将它们放入您的functions.php文件中即可设置.


hel*_*ing 8

通过结合Donal MacArthur的一个稍微修改过的parse_shortcode_content函数解决了这个问题(他最初手动调用wpautop ...我已经删除了.重新排序默认过滤器以便稍后运行wpautop ...在短代码之后已经处理过,而不是之前.

//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;
}
Run Code Online (Sandbox Code Playgroud)

并移动过滤器

//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 );
Run Code Online (Sandbox Code Playgroud)

编辑:

parse_shortcode_content()不再需要该功能(如果有的话).只需调整过滤顺序即可.


che*_*ozo 5

在我的情况下 - 这个解决方案打破了一个侧面短代码(revslider).所以我在这里找到了另一个解决方案:http: //wordpress-hackers.1065353.n5.nabble.com/shortcode-unautop-tp42085p42086.html 这是使用另一个过滤器,如下所示:

// 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');
}
Run Code Online (Sandbox Code Playgroud)

对我来说工作正常:)