首页 文章

如何使用TinyMCE从WordPress编辑器中删除不需要的<p>标签?

提问于
浏览
2

我正在使用WordPress编辑器TinyMCE . 我有这样的事情:

<div class="TA_excellent" id="TA_excellent150"><ul>...</ul></div>
<script type="text/javascript" src="http://www.jscache.com/wejs?wtype=excellent&amp;uniq=150&amp;locationId=287500&amp;lang=en_AU">
</script>

当我跳过可视化编辑器时,“脚本”标签将从内容中删除 . 所以我尝试了各种插件,包括Ultimate TinyMCE,但这次“脚本”标签被“p”标签包裹 .

所以输出是这样的:

...</ul></div>
    <p>
    <script type="text/javascript" src="http://www.jscache.com/wejs?wtype=excellent&amp;uniq=150&amp;locationId=287500&amp;lang=en_AU">
    </script>
    <script src="http://www.tripadvisor.com.au/WidgetEmbed-excellent?uniq=150&amp;locationId=287500&amp;lang=en_AU"></script
    </p>

我还尝试了名为“Advanced TinyMCE Settings”的插件,它允许我更改默认的TinyMCE配置 . 我在TinyMCE设置下的配置是这样的:

extended_valid_elements:  script[type|src],a[*]

我花了几个小时在它上面就行不通了 . 我无法摆脱这个“p”标签 . 他们会继续自动发布 .

以下是Ultimate TinyMCE的截图:

enter image description here

3 回答

  • 1

    删除不需要的p和br标签(空)可以通过向主题函数添加简单的过滤功能来完成.php这是您需要添加的代码 .

    function remove_additional_p($content){
      $content = forec_balance_tags($content);
      return preg_replace('#<p>\s*+(<br\s*/*>)?|s*</p>#i','',$content);
    }
    
    
    add_filter('the_content','remove_additional_p',20,1);
    
  • -1

    在编辑器初始化之前使用此代码删除 <p></p> .

    function tinymce_remove_root_block_tag( $init ) {
        $init['forced_root_block'] = false; 
        return $init;
    }
    add_filter( 'tiny_mce_before_init', 'tinymce_remove_root_block_tag' );
    
  • 1

    它可以在没有代码的情况下完成

    转到 Settings > TINYMCE Advanced 并检查 Stop removing the <p> and
    tags when saving and show them in the HTML editor

    enter image description here

    虽然在您的文本区域中放置脚本是不好的做法,但您可以通过执行以下操作删除 <p> 标记:

    $text=str_ireplace('<p>','',$post->post_conent);
    $text=str_ireplace('</p>','',$post->post_conent);
    

相关问题