WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm using a basic shortcode to wrap some text in an expandable div. What appears to be happening is that the wpautop is inserting a stray closing paragraph tag at the beginning and a stray opening paragraph tag at the end of the content being wrapped.

Here's how it looks in the editor:

[expandable text="Show Content"]

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras fermentum facilisis malesuada. Suspendisse potenti. Aenean dui turpis, ornare in ipsum eget, vestibulum egestas nulla.


And the rendered HTML:

<div class="expandable"><a href="#" class="expand-link">Show Content</a></p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras fermentum facilisis malesuada. Suspendisse potenti. Aenean dui turpis, ornare in ipsum eget, vestibulum egestas nulla.</p>

This is the shortcode function:

function expandable_content_shortcode( $atts, $content = null ) {
    $args = shortcode_atts( array(
        'text' => __( 'View More', $this->plugin_slug ),
        'linklocation' => 'top'
    ), $atts, 'expandable' );

    if ( $args['linklocation'] === 'top' ) {
        $output = '<div class="expandable-content"><a href="#" class="expand-link">' . $args['text'] . '</a>' . $content . '</div>';
    } else {
        $output = '<div class="expandable-content">' . $content . '<a href="#" class="expand-link">' . $args['text'] . '</a></div>';

    return $output;
share|improve this question
remove line breaks within shortcode you've added in editor. – Anjum Sep 3 '13 at 19:20
The shortcode needs to be able to support line breaks within it so I'm not able to just have the author remove outer line breaks. – hereswhatidid Sep 3 '13 at 19:25
then you need to remove wpautop filter or change wpautop execution priority. – Anjum Sep 3 '13 at 19:46
Changing the priority does not solve the issue and removing paragraph and line break display isn't an option. – hereswhatidid Sep 3 '13 at 19:54

This is frustrating issue as I can't depend on the content editors to be savvy enough with Wordpress / HTML to understand how the text they input will be parsed...


Having thought of this a bit more I have reconsidered my earlier answer. HTML5+ comes with the Tidy extension, if you are able to use this on your server then the following will work:

function cleanUpAutoP($content)
        $tidy = new Tidy();
        // Switch out the encoding an doctype in $tidyArgs to suit your use case
        // 'show-body-only' HTML to be parsed as a fragment
        // rather than a whole document
        $tidyArgs = array(  'doctype' => 'html',
                            'input-encoding' => 'utf8',
                            'output-encoding' => 'utf8',
                            'show-body-only' => true
        // Return the repaired string
        return $tidy->repairString($content, $tidyArgs);

This way you can enjoy all the benefits of sweet, sweet P tags without the fear that wpautop will have messed something up. Further options for $tidyArgs can be seen in the documentation.

If Tidy is not available for whatever reason this should still work:

function cleanUpAutoP($content) {

    // Replace all OPENING paragraph tags with <br /><br />
    $content = preg_replace('/<p[^>]*>/', '<br /><br />', $content);

    // Remove all CLOSING p tags
    $content = str_replace('</p>', '', $content);

    return $content;

The obvious drawback of the latter method that I cannot apply styles to p tags in shortcodes, but until a better solution comes along, or the issue is fixed in Wordpress, it will do!

share|improve this answer

Install the WP RAW HTML plugin.

This inserts extra check boxes to the the right of each post in the admin - check the box that says 'Disable automatic paragraph creation', this will solve your problem.

But make sure you have surrounded content on that post with the correct


You can do this by removing a filter, but this solution allows you to only disable it when required and is much easier.

share|improve this answer
This still leaves me with having to force the entire post to be without paragraph formatting which is still not going to work for the editors of the site. – hereswhatidid Sep 6 '13 at 14:41

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.