How to remove unwanted

tags from WordPress edi

2019-05-27 06:46发布

I am using WordPress editor TinyMCE. I have something like this:

<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>

When I skipped to visual editor "script" tags are removed from the content. So I tried every kind plugin including Ultimate TinyMCE but this time "script" tags are wrapped by "p" tags.

So output is something like this:

...</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>

I also tried plugin called "Advanced TinyMCE Settings" it allows me to change the default TinyMCE configuration. My config under TinyMCE settings is like this:

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

I spent hours and hours on it just won't work. I can't get rid of this "p" tags. They keep continue to publish automatically.

Here are screenshots from Ultimate TinyMCE:

enter image description here

3条回答
姐就是有狂的资本
2楼-- · 2019-05-27 07:32

Use this code to remove <p></p> before editor initialize.

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' );
查看更多
ゆ 、 Hurt°
3楼-- · 2019-05-27 07:39

It can be done without code.

Go to Settings > TINYMCE Advanced and check Stop removing the <p> and <br /> tags when saving and show them in the HTML editor

enter image description here

Whereas it is bad practice to put scripts in your text area, well you can remove <p> tags by doing this:

$text=str_ireplace('<p>','',$post->post_conent);
$text=str_ireplace('</p>','',$post->post_conent);
查看更多
We Are One
4楼-- · 2019-05-27 07:49

Removing unwanted p and br tags (empty ) can be done by adding simple filter function to theme functions.php Here is the code you need to add.

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);
查看更多
登录 后发表回答