Kir Mazur
Kir Mazur

Reputation: 1037

Don't remove empty p (<p> </p>) tags from Wordpress on page load

In my basic Wordpress editor I have an article with html-code like this:

<h4><b>Hello!</h4>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>my text</p>

When that article page is loaded I always see only this:

<h4><b>Hello!</h4>
<p>my text</p>

How can I force WordPress to stop removing empty p tags?

I have tried two methods and neither one worked:

1.I tried to add such lines to 'functions.php' of my theme:

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

2.I tried to add slashes to all add_filter( 'the_excerpt', 'wpautop' ); in 'wp-includes/default-filters.php'.

None of these helped. Any ideas how to solve my problem?

Upvotes: 2

Views: 7133

Answers (4)

Marsellus
Marsellus

Reputation: 135

Found here, use <b>&nbsp;</b> to preserve the whitespace. As they state, it's hacky but it works.

Upvotes: 0

Johannes
Johannes

Reputation: 67778

I am using the "TinyMCE Advanced" plugin ( https://wordpress.org/plugins/tinymce-advanced/) to prevent "automatic handling" of <p> tags in the editor.

That way I see the <p> tags which are automatically created in "visual mode" also in the "text mode" tab and can change back and forth between the two modes without loosing any <p> tags, including those which only contain a &nbsp; entity and therefore create an empty line.

Upvotes: 0

Samvel Aleqsanyan
Samvel Aleqsanyan

Reputation: 2960

There are several options to not lose <p>&nbsp;</p>.

  1. Use only text editor: every time you're switching from Text editor to Visual you'll lose changes.

  2. Use filter:

    function prevent_deleting_pTags($init){
        $init['wpautop'] = false;
    
        return $init;
    }
    
    add_filter('tiny_mce_before_init', 'prevent_deleting_pTags');
    

    Code goes to functions.php file of your active theme.

  3. Add some id/class/data- to your p tag:

    <p class="my_class">&nbsp;</p>
    <p id="my_id">&nbsp;</p>
    <p data-save="my_save">&nbsp;</p>
    

Upvotes: 3

BenoitLussier
BenoitLussier

Reputation: 204

This is due to the way your theme handles empty paragraphs. If the extra paragraphs are for spacing, the best thing to do would be to add a css id or class to the last paragraph and add the proper spacing with margin or padding.

I wouldn't add empty paragraphs so I have never had this problem, however I have seen some themes that automatically remove the <p> and </p> if the paragraph does not have a class or id. You could try the following to force WordPress to leave the paragraphs as is:

<h4><b>Hello!</h4>
<p class="blank">&nbsp;</p>
<p class="blank">&nbsp;</p>
<p class="texthere">my text</p>

Also, that <b> has to be closed eventually...

Upvotes: 0

Related Questions