0

Here's my very simple shortcode:

function box_shortcode( $atts, $content = null ) {
        extract( shortcode_atts( array(
            'width' => 'auto',
            'height' => 'auto', 
            ), $atts ) );  

       return '<div class="boxy-box" style="width:'. $width .'; height:'. $height .';" >'.do_shortcode($content).'</div>';
    }

    add_shortcode('box', 'box_shortcode'); 

[box]something[/box]

should give:

<div class="boxy-box" width:auto; height:auto;>something</div>

But, it doesn't. Even an empty shortcode (without a content, eg. [box][/box]) gives:

<div class="boxy-box" width:auto; height:auto;><br/></div>

(I don't care, but don't know where the br comes from)

The main problem is that, when I try [box]something[/box] I'm getting:

<div class="boxy-box" width:auto; height:auto;><br/>something<br/></div>

I know WP editor formats text automatically and there are "noP" plugins out there, but firstly - I'm using the HTML editor, secondly, why the heck WP is adding Brs (sometimes p's) to an inline code? :O

It wouldn't be a huge problem, but I HAVE to get PURE output, so:

[box]<h1>Header</h1><div>Content</div>[/box]

Will give a header and a div inside my box div, without any parapgrahs, brs, spans etc.

The funny thing is I have the same shortcode with h2 instead of div and it doesn't give any brs etc.

Sometimes I'm also getting a <div class="boxy-box" width:auto; height:auto;></p>$content (...)

[edit]

I've found some comments on it:

This is driving me nuts as well. What the f**k is the point of having a html/code mode for posting if wordpress filters the god damn code and wraps around the tags anyways?

So I guess that's the problem. Is there a way of turning autop functionality at least for HTML editor? ;/

1
  • Is there a particular reason you call do_shortcode inside the shortcode function, are you expecting other shortcodes inside your own? If not, i'd suggest testing minus the do_shortcode call.
    – t31os
    Mar 7, 2011 at 14:30

3 Answers 3

2

You can postpone the wp_autop filter. Wordpress has this filter enabled by default. And it is processing before the shortcode output.

remove_filter( 'the_content', 'wpautop' );
add_filter( 'the_content', 'wpautop' , 12);

Add this to your functions.php and check if the problem persist!

See a similar problem here: stray <p> elements

4
  • actually I did that, but I believe it won't create ps while writing posts anymore? :( Mar 7, 2011 at 13:15
  • 1
    ok, got it. +30 points for You! Mar 7, 2011 at 15:18
  • I'm not sure if you're still here, but I have some wpautop trouble :) Here: wordpress.stackexchange.com/questions/15137/formatting-code Apr 19, 2011 at 21:25
  • 1
    I have seen a few "solutions" to this problem on SE and other places, and none of them have worked until I found yours. I wish I could +100 this answer and bitchslap the authors of the other answers. Sep 11, 2012 at 18:45
2

I had the same problem and checked the $content in the add_filter() function. There you can see what happen with the shortcodes. I ended up with an easy plugin, that removes p and br tags on shortcodes: http://www.johannheyne.de/wordpress/shortcode-empty-paragraph-fix/

0

I tried this:

remove_filter( 'the_content', 'wpautop' );
add_filter( 'the_content', 'wpautop' , 12);

The filter works but messes up Contact Form 7

I tried the plugin code and it worked perfect!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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