Make WordPress Core

Opened 7 years ago

Closed 5 years ago

#43387 closed defect (bug) (duplicate)

wpautop() adds "</p>" where it should not

Reported by: 1994rstefan's profile 1994rstefan Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.0
Component: Formatting Keywords:
Focuses: Cc:

Description

There are a few cases where wpautop() creates invalid HTML code by inserting just a closing p tag. (Opening tag is missing)

The problem exists with all block elements (defined inside wpautop function)

$allblocks = '(?:table|thead|tfoot|caption|col|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|form|map|area|blockquote|address|math|style|p|h[1-6]|hr|fieldset|legend|section|article|aside|hgroup|header|footer|nav|figure|figcaption|details|menu|summary)';

I have found two cases where this happens but there may be more.

Minimal Examples:

echo wpautop('<div>X<div></div></div>');
echo PHP_EOL;
echo wpautop('<div><div></div>
 </div>'); // note the space in front of the tag

Output:

<div>X</p>
<div></div>
</div>

<div>
<div></div>
</p></div>

The problem is relevant because wpautop is used to filter the_content().

Examples what the user could insert inside the backend editor which results in this error:

<h1>Heading<div class="subheading">Subheading</div></h1>

<div class="allign-center">
    <div class="image-wrap">
        <img src="about:blank" alt="example">
        <div class="image-description">just an example</div>
    </div>
</div>

Change History (3)

#2 @dionysous
6 years ago

We encounter this problem on our sites, too. Users reported, that </p> is added after an <img> tag, which comes right before an opening <div> tag.

Input in the editor:

<div class="image-grid"><img class="alignnone size-full wp-image-570" src="{{IMG_URL}}" alt="" />
<div class="overlay"><img class="alignnone size-full wp-image-573" src="{{IMG_URL}}" alt="" /></div>
</div>

Output in frontend:

<div class="image-grid"><img class="alignnone size-full wp-image-570" src="{{IMG_URL}}" alt="" /></p>
<div class="overlay"><img class="alignnone size-full wp-image-573" src="{{IMG_URL}}" alt="" /></div>
</div>

It is quite easy to reproduce.

We run automatted tests on our websites, invalid HTML is shown as an error. This happening quite often. The users ask us to fix this problem, but it seems to be a bug in the WP core, more precisely in the wpautop function.

#3 @pento
5 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to duplicate
  • Status changed from new to closed

Duplicate of #27350.

Note: See TracTickets for help on using tickets.