Editor removes <p> tags

How do I stop the editor from stripping my <p> tags and “empty (& nbsp;)” divs on pages?

Since @scribu asked for a sample code here it is:

Read More

Input:

<p>text</p>
<div>&nbsp;</div>

Output:

text

Related posts

Leave a Reply

7 comments

  1. I had problems with TinyMCE Advanced. I struggled with this for a while. Finally discovered a simple solution – Use Shortcodes!

    Place this code into functions.php and enter [br] wherever you want a br tag to appear.

    add_shortcode("br", "br_tag");
    
    function br_tag(){
            return("<br/>");                            
    }
    
  2. 1) Try a few different plugins that disable formatting and stop WP’s built-in stripping of extra paragraphs and whitespace: http://wordpress.org/extend/plugins/search.php?q=formatting&sort=

    2) You can fool WP into adding a paragraph break by using <b>&nbsp;<b/> in the html editor. It’s a non-breaking space in <b> tags. You won’t be able to see it in the visual editor, so add it in the htnl editor. It’s ugly, but it works without needing to fully disable formatting.

    3) You can also do this in functions.php and then wrap the text you don’t want formated with <!-- noformat on --> and <!-- noformat off --> tags.

    function newautop($text)
    {
        $newtext = "";
        $pos = 0;
    
        $tags = array('<!-- noformat on -->', '<!-- noformat off -->');
        $status = 0;
    
        while (!(($newpos = strpos($text, $tags[$status], $pos)) === FALSE))
        {
            $sub = substr($text, $pos, $newpos-$pos);
    
            if ($status)
                $newtext .= $sub;
            else
                $newtext .= convert_chars(wptexturize(wpautop($sub)));      //Apply both functions (faster)
    
            $pos = $newpos+strlen($tags[$status]);
    
            $status = $status?0:1;
        }
    
        $sub = substr($text, $pos, strlen($text)-$pos);
    
        if ($status)
            $newtext .= $sub;
        else
            $newtext .= convert_chars(wptexturize(wpautop($sub)));      //Apply both functions (faster)
    
        //To remove the tags
        $newtext = str_replace($tags[0], "", $newtext);
        $newtext = str_replace($tags[1], "", $newtext);
    
        return $newtext;
    }
    
    function newtexturize($text)
    {
        return $text;   
    }
    
    function new_convert_chars($text)
    {
        return $text;   
    }
    
    remove_filter('the_content', 'wpautop');
    add_filter('the_content', 'newautop');
    
    remove_filter('the_content', 'wptexturize');
    add_filter('the_content', 'newtexturize');
    
    remove_filter('the_content', 'convert_chars');
    add_filter('the_content', 'new_convert_chars');
    
  3. Add in your functions.php

    remove_filter ('the_content', 'wpautop');
    remove_filter ('the_excerpt', 'wpautop');
    

    But you have to add no <p> and <br/> manually. These tags are not saved in the database.

  4. It’s an older question, but this might help you or someone else for a part:

    add_filter('wp_insert_post_data', function ($data, $postarr) {
        $data['post_content'] = wpautop($data['post_content']);
    
        return $data;
    }, 10, 2);
    

    This adds the paragraph tags before saving. And of course the editor still understands how to process it 😉

    It can’t help you with &nbsp;, but you could ask yourself if they are necessary and maybe you can solve that with css? Just trying to help. Or use the hack (<b>&nbsp;<b/>) explained by @markratledge

  5. Bit of a necro, but for the <p> tag you can add a class or perhaps another property.

    e.g.

    <p class="nothing-relevant">Foo</p>

    will not be stripped (using Gutenberg).