Automatically added brs and paragraphs?

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');
This is box title
something

should give:

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

But, it doesn’t. Even an empty shortcode (without a content, eg.

This is box title

) 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

This is box title
something

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:

<div class="su-box su-box-style-default" id="" style="border-color:#000000;border-radius:3px"><div class="su-box-title" style="background-color:#333333;color:#FFFFFF;border-top-left-radius:1px;border-top-right-radius:1px">This is box title</div><div class="su-box-content su-u-clearfix su-u-trim" style="border-bottom-left-radius:1px;border-bottom-right-radius:1px"><h1>Header</h1><div>Content</div></div></div>

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? ;/

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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

Method 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/

Method 3

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!


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x