【问题标题】:Disable automatic formatting inside WordPress shortcodes禁用 WordPress 短代码中的自动格式化
【发布时间】:2011-05-09 18:23:19
【问题描述】:

我已经看过有关创建(原始)简码的教程,该简码不会影响其中的代码,

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

但不幸的是,这一次只适用于一个短代码...... 并且因为else 语句绕过了正常的过滤器并调用了函数方向。我对 autop 和 texturize 函数的其他修改被忽略了。

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

【问题讨论】:

  • 链接(实际上)已损坏。它重定向到一个通用页面,https://wphacks.com/ 的主页。

标签: wordpress


【解决方案1】:

在多个网站上实施@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 文件中即可。

【讨论】:

  • 感谢您的验证。我已接受此答案,以便人们停止使用我的完整代码。
  • shortcode_unautop 在哪里定义?它是 Wordpress 的一部分吗?
  • 说真的,为什么这不是默认值?我从一个通过 wpautop 运行两次的短代码中得到了各种奇怪的错误输出——把它放进去,它很完美。
  • @NateBeaty 我想这只是 WordPress 的另一个奇怪的怪癖。 wpautop() 是那些看起来相当过时的函数之一,但这些行为让它有点烦人。
  • 有人记得functions.php的哪一部分吗?
【解决方案2】:

我通过结合来自 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() 函数(如果曾经需要的话)。只需调整过滤器顺序。

【讨论】:

  • 过滤器重新排序后,我什至不确定是否需要继续使用 parse_shortcode_content($content) 代替嵌套短代码中的 do_shortcode($content)
  • 谢谢,但你在哪里调用 parse_shortcode_content?如果我将该代码放在functions.php 中,则不会发生任何事情。谢谢
  • 老实说,一年后,我真的不记得了。我相信它在简码函数内......代替 do_shortcode()。但尝试重新排序过滤器,这似乎适用于@dan
  • 这段代码看起来不对。一个 4 个字符长或 3 个字符长的字符串怎么可能是 ''(一个空字符串)?
  • 我不知道。它可能不再正确,甚至不再需要。
【解决方案3】:

在我的情况下 - 这个解决方案破坏了其中一个侧简码 (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');
}

对我来说很好用:)

【讨论】:

    【解决方案4】:

    请在此处查看我的答案:

    Remove wpautop from shortcode content / remove whitespace in buffering

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-18
      • 2021-02-24
      • 2011-11-24
      • 2018-04-27
      • 2011-03-26
      • 2012-03-13
      • 2013-08-18
      • 1970-01-01
      相关资源
      最近更新 更多