How to remove unwanted <p> tags from WordPress editor using TinyMCE?

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.

Read More

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

Related posts

Leave a Reply

3 comments

  1. 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*+(<brs*/*>)?|s*</p>#i','',$content);
    }
    
    
    add_filter('the_content','remove_additional_p',20,1);
    
  2. 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' );
    
  3. 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);