Radmation
Radmation

Reputation: 1534

Wordpress Creating Empty <p> Tags?

It seems that wordpress is creating empty p tags for no reason whatso ever. I usually correct this problem by getting rid of all white space - as white space seems to be interpreted as "P TAG HERE! PLACE A P TAG HERE".

Here is my Wordpress dashboard content that I am concerned with:

[block]<p>This is a test</p>[/block]

As you can see there is no whitespace.

Here is the HTML Representation of that same piece of html.

<div class="block sidebar">
<p>This is a test</p>
<p></p></div>

See that second <p></p> that is there for no reason?

Can someone please tell me why this happens and how (if at all) I can remedy the situation?

I created that shortcode [block] as well and have been using it on other pages without this problem. So it seems page specific (because that makes sense..not).

Any help is very much appreciated. I can provide a link to the page if necessary. Let me know.

How the dashboard looks:

enter image description here

Upvotes: 4

Views: 5870

Answers (2)

Radmation
Radmation

Reputation: 1534

The reason why wordpress does tried to format code is because of a function called wpautop https://codex.wordpress.org/Function_Reference/wpautop

I found a solution from this thread and tweaked it a bit: Wordpress - empty p tags

add_filter('the_content', 'remove_empty_p', 11);
function remove_empty_p($content){
    $content = force_balance_tags($content);
    //return preg_replace('#<p>\s*+(<br\s*/*>)?\s*</p>#i', '', $content);
    return preg_replace('#<p></p>#i', '', $content);
}

The third parameter 11 is the priority. For me wpautop filter has a priority of 10 so I set my filter to 11 and that solved my problem.

Thanks all who have tried to help!

Upvotes: 7

Stanimir Stoyanov
Stanimir Stoyanov

Reputation: 1916

Add this to your functions.php and it will fix the problem

add_filter('the_content', 'shortcode_empty_paragraph_fix');
function shortcode_empty_paragraph_fix($content) {
    $array = array(
        '<p>['    => '[',
        ']</p>'   => ']',
        ']<br />' => ']',
        ']<br>'   => ']',
    );

    $content = strtr($content, $array);

    return $content;
}

Upvotes: 2

Related Questions