5

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:

Input:

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

Output:

text
  • 4
    Why the negative points? – Eeyore Mar 8 '11 at 1:52
  • just use this plugin its has built in option to auto remove p and br tag wordpress.org/extend/plugins/tinymce-advanced – user7580 Aug 3 '11 at 10:54
  • Come on surely in the past 8 years there must have been a solution to this.... considering I'm experiencing it too and there seems to be no reason why any <p> elements should be stripped. Why is this happening still? – Lee May 21 '19 at 9:11
4

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/>");                            
}
  • 1
    Haha, that's Wordpress in a nutshell. – qwerty Nov 27 '12 at 9:39
1

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');
1

I know this is a fairly old forum...I've come across many on this issue of visual editor stripping tags when switiching tabs...I simple followed this link: http://rubayathasan.com/reviews/enabling-line-break-in-wordpress/#comment-1349250

and installed TinyMCE Advanced...all is working like a charm and I now have more features to be able to add to my visual editor.

0

If you're going to be adding HTML tags, why not just use the HTML Editor?

  • 4
    and how do we do that ? – jeff Sep 30 '13 at 10:13
  • FYI, tis is what I'm using, and it's still stripping <p> tags surrounding shortcodes on the front end. No action is happening within the shortcode, but it's strange that they are disappearing for no reason. – Lee May 21 '19 at 9:10
0

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.

0

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

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.