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

发布于 2024-11-06 04:28:40 字数 449 浏览 0 评论 0原文

我看过有关创建(原始)短代码的教程,该短代码使其中的代码保持不变,

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

但不幸的是,这一次仅适用于一个短代码...,因为 else 语句绕过普通过滤器并调用函数指令。我对 autop 和texturize 函数的其他修改被忽略。

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

安人多梦 2024-11-13 04:28:40

在多个网站上实施 @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);

将它们放入您的 functions.php 文件中,然后就可以了。

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.

把回忆走一遍 2024-11-13 04:28:40

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

// 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;
}

并且移动过滤器

// 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 );

编辑:

不再需要parse_shortcode_content()函数(如果曾经需要)。 。

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.

∝单色的世界 2024-11-13 04:28:40

就我而言 - 这个解决方案破坏了侧面短代码之一(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');
}

它对我来说效果很好:)

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');
}

It works fine for me :)

假装不在乎 2024-11-13 04:28:40

请在此处查看我的答案:

从短代码内容中删除 wpautop/删除缓冲中的空格

它允许您使用以下命令关闭任意数量的特定短代码的 wpautop:

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

Please see my answer here:

Remove wpautop from shortcode content / remove whitespace in buffering

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'));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文