[wp-trac] [WordPress Trac] #34722: Open P Tag in shortcode related to h tag
WordPress Trac
noreply at wordpress.org
Sun Nov 29 07:19:00 UTC 2015
#34722: Open P Tag in shortcode related to h tag
--------------------------+------------------------------
Reporter: BackuPs | Owner:
Type: defect (bug) | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Formatting | Version: trunk
Severity: normal | Resolution:
Keywords: close | Focuses:
--------------------------+------------------------------
Comment (by StephenCronin):
I've looked into this a little further and it's complicated.
Shortcode content has wpautop applied to it, then shortcode_unautop
applied to it which undoes the wpautop changes. But in this case,
shortcode_unautop doesn't roll back all the changes made in wpautop.
The wpautop function has one line which adds "a single line break above
block-level opening tags" and another line which adds "a double line break
below block-level closing tags". If I comment out those lines, I get this:
{{{<div class="theme-div"><h3>This is the test heading</h3> This is the
test body </div>}}}
So they are the lines which are behind the extra br and the unclosed p
tag. They are necessary for non shortcode content, but they should be
rolled back for shortcodes by shortcode_unautop and it's not currently
doing that. I'm not a Regex ninja, so I'm not sure how to change
shortcode_unautop to do this.
But....
Turns out there are quite a few tickets out there about shortcodes and
wpautop, which are at least related to this (if not exactly the same).
[https://core.trac.wordpress.org/ticket/14050 One of them] is 5 years old
and has a patch. [https://core.trac.wordpress.org/ticket/6984 This one] is
8 years old and is "slated for 4.5 Milestone" as part of @miqrogroove 's
[https://make.wordpress.org/core/2015/10/08/shortcode-roadmap-draft-three/
roadmap proposal] for an alternative shortcode syntax.
So, I would expect this to get fixed at some point, maybe 4.5, but as part
of the larger reworking of shortcodes.
In the meantime, it's hacky, but you could use this:
{{{
function theme_shortcode_div( $atts, $content = null, $code ) {
$content = str_replace( '<p></p>', '', wpautop( $content) );
return '<div class="theme-div">' . $content . '</div>';
}
add_shortcode( 'theme_div', 'theme_shortcode_div' );
}}}
This applies wpautop to the shortcode content, adding the missing closing
p tag and converting the br tag to {{{<p></p>}}} and then manually strips
that out.
Of course, you probably want to test that to make sure it works for you.
It should fix this particular problem, but may cause other problems with
different markup in the shortcode content - I haven't tested it with
anything else.
--
Ticket URL: <https://core.trac.wordpress.org/ticket/34722#comment:13>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list