WordPress wpautop issue

2019-04-12 22:04发布

问题:

I have disabled wpautop through the given code:

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

This function works. But my actual problem is that, it removes custom "p" tags that I have manually used in the content.

So the problem is that when I don't use the above given code, there are automatic p tags that destroy my website and when I disable them, custom p tags are also disabled.

回答1:

The easiest and simplest way to avoid wpauto issue is to understand how wpautop works,

Any plain text, or inline element that aren't wrap with block element will be automatically wrap with p. if element is block and doesn't have two line breaks inside it, it won't be wrap with P as well as the elements inside it, empty inline element will be strip and remove.

example

inline

<img src="blabal.jpg"> will be output <p><img src="blabal.jpg"></p>,

I am awesome will output <p>I am awesome</p>,

<span>Really?</span> will output <p><span>Really?</span></p>,

block

<div><img src="blabal.jpg"></div> will be output <div><img src="blabal.jpg"></div>,

<div>I am awesome</div> will output <div>I am awesome</div>,

<div><span>Really?</span></div> will output <div><span>Really?</span></div>,

<span></span> will output nothing, (empty inline elements will be removed.),

I really like wpautop, I never disable it, I just wrap any content with DIV if there's an element I don't want to have P, I also have a shortcode that avoids wpauto when element is inside, its helpful when you have a long ass content that wanted to avoid automatic p tag



回答2:

Here's a fix/work-around I just found through experimentation. Give the <p> element an attribute (<p class>). It doesn't even have to have an actual value. WordPress seems to think that if it has an attribute, it's worth keeping. Furthermore, the attribute doesn't even have to be an actual HTML attribute. For example, <p data-please-work> seems to work just fine. I'm not saying I necessarily recommend doing that, but it's worth mentioning.


I actually discovered this problem myself just now, and searching for a fix lead me to this question. It's a very exasperating situation, and I don't understand why it works like that. You would think that disabling it would simply prevent WordPress from adding its own <p> elements—but no. It seems like it tries to prevent <p> elements, period—even yours.

If someone else has another way to go about dealing with this, I'd love to know. Silver's tips seem helpful to know though.