【问题标题】:remove empty <p> tags from wordpress shortcodes via a php functon通过 php 函数从 wordpress 短代码中删除空的 <p> 标签
【发布时间】:2012-11-10 16:47:56
【问题描述】:

寻找一种 php 函数(非 jQuery 或 wpautop 修改)方法从 wordpress 中删除 &lt;p&gt;&lt;/p&gt;

我试过了,但它不起作用:

        function cleanup_shortcode_fix($content) {   
          $array = array (
            '<p>[' => '[', 
            ']</p>' => ']', 
            ']<br />' => ']',
            ']<br>' => ']'
          );
          $content = strtr($content, $array);
            return $content;
        }
        add_filter('the_content', 'cleanup_shortcode_fix');

【问题讨论】:

  • 括号和标签之间的内容中是否有空格或空格(\n 等)?
  • 当他的以括号开头的段落突然变成断段落时,编辑会不会感到惊讶..
  • @DamienOvereem:has_shortcode 的实现可以解决这个问题。
  • 只是想指出,如果我在 add_filter 的末尾添加 10 个优先级,上述代码对我有用,例如 add_filter('the_content', 'cleanup_shortcode_fix',10,1);

标签: php wordpress function shortcode


【解决方案1】:

尝试将此代码插入您的 functions.php 文件中:

remove_filter( 'the_content', 'wpautop' );
add_filter( 'the_content', 'wpautop', 99 );
add_filter( 'the_content', 'shortcode_unautop', 100 );

【讨论】:

  • 我也试过这个解决方案,但是我发现了另一个问题,它在其他地方生成了P标签。
  • 效果很好!我不确定将代码放在哪里,所以我对其进行了测试并添加了屏幕截图。非常感谢!
【解决方案2】:

这是一个老问题,但我今天解决了这个问题并想分享一下。

就我而言,我基本上想删除所有格式不佳的 &lt;p&gt;&lt;br&gt; 标记,但随后您想正确添加它们,以便短代码中的文本格式正确。

/*
 * Half column shortcode
 */
    function custom_shortcode_half_column( $atts, $content = '') {
        $content = custom_filter_shortcode_text($content);
        return '<div class="half-column">'. $content .'</div>';
    }
    add_shortcode( 'half-column', 'custom_shortcode_half_column' );


/*
 * Utility function to deal with the way WordPress auto formats text in a shortcode.
 */
    function custom_filter_shortcode_text($text = '') {
        // Replace all the poorly formatted P tags that WP adds by default.
        $tags = array("<p>", "</p>");
        $text = str_replace($tags, "\n", $text);

        // Remove any BR tags
        $tags = array("<br>", "<br/>", "<br />");
        $text = str_replace($tags, "", $text);

        // Add back in the P and BR tags again, remove empty ones
        return apply_filters('the_content', $text);
    }

在我看来,这确实应该是 WordPress 解析简码 $content 参数的默认方式。

【讨论】:

    【解决方案3】:

    你可以删除

    标签输入

    <?php echo $post->post_content; ?>
    

    而不是 the_content()

    【讨论】:

      【解决方案4】:

      您需要的是 jquery 和 php 的混合......这是唯一的工作方式
      我发现工作得很好。我的网站上有教程,但是
      为了把东西放在这里

      jQuery:
      将它包含在您已经入队的某个 JS 文件中

      jQuery(function($){
          $('div#removep > p').filter(function() {
              return $.trim($(this).text()) === '' && $(this).children().length == 0
          })
          .remove()
      })
      

      您以后可以使用的短代码:
      在您的 functions.php 或包含的文件中

      function sght_removep( $atts, $content = null ) {return '<div id="removep">'.do_shortcode($content).'</div>';}
      add_shortcode('removep', 'sght_removep');
      

      现在您可以像这样包装特定的东西:

      [removep]
      Some text i write directly in wordpress wysiwyg
      <p></p> <-- this would get removed
      [/removep]
      

      此解决方案需要一些知识,但它确实有效!
      希望这会有所帮助...

      【讨论】:

      • 这很可能是我在 SO 上见过的最糟糕的答案。它没有回答问题,因为 OP 明确表示不是 jQuery,这是一个糟糕的解决方案,因为您永远不应该在 Javascript 中修复错误的标记,特别是当您有机会在 PHP 提供服务之前在 PHP 中修复它时。
      • 你错了!在许多情况下,您不想进行全局修复,而是在写帖子时进行简单的有针对性的修复!您不应该仅仅为了修复一个列结构或您想要定位的其他元素而更改 WP 的整个输出。在许多情况下,您没有“糟糕的标记”——您只想定位特定元素……(顺便说一句:感谢戏剧)。
      • 谁说过全局修复?您可能只需要修复一个或几个短代码,我明白了。而且我在此页面上没有看到完美的答案。但是一个糟糕的 JS hack 并不是答案。 WP 已经被太多“我刚刚做了这个糟糕的 hack,它对我有用”的代码建议所困扰。
      • 这是一个 gr8 解决方案......短代码和其他地方的不想要的 P 标签的问题会一次又一次地出现......在某些情况下它可以 - 有时甚至是 gr8,这就是为什么关闭它(这很容易)不是每个人的最佳解决方案!但有时它会打扰,所以一个小的短代码是解决特定问题的 gr8 方法 - 此外,我认为使用 jQuery 自定义元素以及它们如何出现没有问题。这在当今的网页设计世界中很常见。
      【解决方案5】:

      add_filter('the_content', 'cleanup_shortcode_fix', 10);

      我发现如果您指定 10 作为优先级,它会起作用;没有其他号码可以工作。

      【讨论】:

        【解决方案6】:

        您应该提高过滤器的优先级。

        这应该可以工作

        add_filter('the_content', 'cleanup_shortcode_fix', 1);
        

        【讨论】:

          【解决方案7】:

          也许正则表达式可以工作:

          $string=preg_replace_('/<p>\s*</p>/', '', $string);
          

          这应该将任何 &lt;p&gt;&lt;/p&gt; 替换为空,或者只是将其中的空格替换为空,从而将它们删除。

          将正则表达式应用于 HTML 代码时,最好先删除 HTML 的 \r\n,因为它们会阻止正则表达式的工作。

          【讨论】:

          • 这没有帮助,因为删除内容中的所有p标签,有时p标签有样式
          猜你喜欢
          • 2013-01-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多