Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 403
Radmation Avatar asked Oct 29 '25 08:10

Radmation


1 Answers

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!

like image 136
Radmation Avatar answered Oct 31 '25 08:10

Radmation