Make WordPress Core

Opened 11 years ago

Closed 11 years ago

#25415 closed defect (bug) (worksforme)

magically (improperly!) inserted P tag

Reported by: crysman's profile crysman Owned by:
Milestone: Priority: normal
Severity: major Version: 3.6.1
Component: Formatting Keywords: reporter-feedback close
Focuses: Cc:

Description

I've encountered an interresting bug today, which occurs whenever you insert more than one shortcode in the edit page admin interface.

I've been using latest WP (3.6.1), standard twenty-thirteen template and have disabled all plugins except for this one I am using for testing:

<?php
/*
Plugin Name: test shortcode
Description: just a simple shortcode test
*/

function testshortcode() {
    return 'SUCCESS!';
}
add_shortcode('testshortcode', 'testshortcode');

?>

Now try these 4 variants in the Edit page admin interface:
1)

[testshortcode]

2)

[testshortcode][testshortcode]

3)

[testshortcode]
[testshortcode]

4)

[testshortcode]

[testshortcode]

Examples 2 and 3 will magically (and improperly!) wrap the output of the shortcode to P tag!

Let's look into the HTML source of the published page (all whitespace has been preserved):
1)

</header><!-- .entry-header -->

					<div class="entry-content">
						SUCCESS!
											</div><!-- .entry-content -->

					<footer class="entry-meta">

2)

</header><!-- .entry-header -->

					<div class="entry-content">
						<p>SUCCESS!SUCCESS!</p>
											</div><!-- .entry-content -->

					<footer class="entry-meta">

3)

</header><!-- .entry-header -->

					<div class="entry-content">
						<p>SUCCESS!<br />
SUCCESS!</p>
											</div><!-- .entry-content -->

					<footer class="entry-meta">

4)

</header><!-- .entry-header -->

					<div class="entry-content">
						SUCCESS!
SUCCESS!
											</div><!-- .entry-content -->

					<footer class="entry-meta">

Both examples 2 and 3 are wrong, because there is the P tag wrapping added out of nowhere...

Shortcodes should be not wrapped in P or anything. If I wanted it wrapped, I would do it myself...

This makes some of our pages INVALID HTML, and should certainly be fixed.

Change History (5)

#1 @crysman
11 years ago

I've made a small mistake here, it's not the shortcode output what is being wrapped by P, it is the whole page content.

See this example:
Editor source (in "Text" mode):

aaa
[testshortcode]aaa[testshortcode]
aaa

Published HTML output:

</header><!-- .entry-header -->

					<div class="entry-content">
						<p>aaa<br />
SUCCESS!aaaSUCCESS!<br />
aaa</p>
											</div><!-- .entry-content -->

					<footer class="entry-meta">

#2 @SergeyBiryukov
11 years ago

  • Component changed from General to Formatting
  • Keywords reporter-feedback added

Related: #24085

I don't see anything wrong with the examples 2 and 3 and comment:1, it's still valid markup and the default behaviour of wpautop(). Do you have an example where it actually produces invalid HTML?

#3 @crysman
11 years ago

Yes, here is the real example:

<article class="post group">
<header>
<!-- <h1><a href="#" rel="bookmark" title="link to this post" title="odkaz na tuto stránku">Přihlášení</a></h1>-->
</header>
<!-- debug -->
<pre>
</pre>
<!-- //debug -->
<p><h1>Přihlášení do Lužáneckého profilu</h1>
<br /><form action="/profil/prihlaseni/" method="POST" id="form-prihlasovani" >
<table><tbody>
<tr>
<th><label for="form-email">Přihlašovací e-mail: </label></th>	 <td><input type="email" name="email" id="form-email" value="" placeholder="@" required></td>	 </tr>
<tr>
<th><label for="form-heslo-nevalidovat">Heslo: </label></th>	 <td><input type="password" name="password" id="form-heslo-nevalidovat" required></td>	 </tr>
<tr>
<td colspan="2">
<input type="submit" value=" Přihlásit "> &nbsp;	 <a href="xxx">Zapomněli jste heslo?</a>	 &nbsp; | &nbsp;	 <a href="xxx">Registrace</a>	 </td>	 </tr>
</tbody></table>
<input type="hidden" name="redirect_to" value="">	<input type="hidden" name="xxx" value="xxx" />
</form>
<br /></p>
</article>

Here is the W3C HTML validator reported markup error:

Error Line 207, Column 10: No p element in scope but a p end tag seen.
<br /></p>

In the code above it is the line just before the ending ARTICLE tag. It is very probably because TABLE doesn't like to be in P element...

PS: we are using this in the page content:

[shortcode1]
[shortcode2]
Last edited 11 years ago by crysman (previous) (diff)

#4 @knutsp
11 years ago

  • Keywords close added

If your content is solely created by one or two shortcodes that expands to block level elements then you have no benefit from wpautop().

remove_filter( 'the_content', 'wpautop' );

Then write a function that determines when you don't need this formatting, and returns accordingly:

function my_formatting( $content ) {
    if ( test-expression-here ) 
        return $content;
    else
        return wpautop( $content );
}
add_filter( 'the_content', 'my_formatting' );

I guess you will have to test for either post_type, post_format, category, post_tag or analyze the content to find out.

Another way is to disable wpautop() manually on a post by post basis, with a plugin like this http://wordpress.org/plugins/wpautop-control/

WordPress runs wpautop at priority 10 and shortcodes at priority 11 (hence, after wpautop). It's possible to manipulate this order, bringing your specific shortcodes to run before wpautop. wpautop() will then see the html block level elements and leave them as they are, while allowing extra text to go into paragraphs http://www.viper007bond.com/tag/wpautop/.

Further on this matter is a support thing http://wordpress.org/support/. This is not a bug.

#5 @nacin
11 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to worksforme
  • Status changed from new to closed
Note: See TracTickets for help on using tickets.