2199 posts
  • Has referred 200+ members
  • Has sold $500,000+ on Envato Market
  • Has collected 50+ items on Envato Market
  • Elite Author: Sold more than $75,000 on Envato Market
+13 more
mpc says
Hi, guys few months back I was looking on the forums for a solution for empty paragraph tags and breaks at the beginning of the shortcode. I have found this solution which works very good:
/*-----------------------------------------------------------------------------------*/
/*    Shortcodes clean up - prevents the empty paragraph at the top of the shortcode
/*-----------------------------------------------------------------------------------*/

// clean up shortcode

function parse_shortcode_content( $content ) {

   /* Parse nested shortcodes and add formatting. */
    $content = trim( do_shortcode( shortcode_unautop( $content ) ) );

    /* Remove '' from the start of the string. */
    if ( substr( $content, 0, 4 ) == '' )
        $content = substr( $content, 4 );

    /* Remove '' from the end of the string. */
    if ( substr( $content, -3, 3 ) == '' )
        $content = substr( $content, 0, -3 );

    /* Remove any instances of ''. */
    $content = str_replace( array( '<p></p>' ), '', $content );
    $content = str_replace( array( '<p>  </p>' ), '', $content );

    return $content;
}

// move wpautop filter to AFTER shortcode is processed

remove_filter( 'the_content', 'wpautop' );
add_filter( 'the_content', 'wpautop' , 99);
add_filter( 'the_content', 'shortcode_unautop',100 );

I have used it in few of my themes and everything was fine. But now with my new theme the reviewer said that he wont accept my theme with this code :O So my question is there any other solution to avoid those empty p and br tags? What do you think about this?

384 posts
  • Has sold $40,000+ on Envato Market
  • Has been a beta tester for an Envato feature
  • Has been part of the Envato Community for over 4 years
  • Sells items exclusively on Envato Market
+2 more
kaaz says

why WP core is inserting empty tags??? WordPress dev guys should fix this ASAP

122 posts WordPress Wizard
  • Elite Author: Sold more than $75,000 on Envato Market
  • Made it to the Authors' Hall of Fame
  • Had an item featured on Envato Market
  • Won a Most Wanted contest
+5 more
CODE9RS says

Hmm…. Don’t have sure if the reviewers are the real idiots and amateurs here…. hahahahahhaa

The reviewers have a big experience and I think you should respect it…

The reviewer is doing the write thing, this kind of code @mpc modifies the default behavior of WP filters. You can’t do it, if you do, some popular plugins can’t work fine.

I’m using this code now:

     add_filter('the_content', 'shortcode_empty_paragraph_fix');

    function shortcode_empty_paragraph_fix($content)
    {   
        $array = array (
            '<p>[' => '[', 
            ']</p>' => ']', 
            ']<br />' => ']'
        );

        $content = strtr($content, $array);

        return $content;
    }

Add this to your functions.php for example.

This one it’s fine, because doesn’t strip any default filter, only add a additional filter what is acceptable.

I hope it helps :)

194 posts Dreams Money Can Buy
  • Has sold $1M+ on Envato Market and is now a Power Elite Author
  • Power Elite Author: Sold more than $1M on Envato Market
  • Has referred 200+ members
  • Has collected 10+ items on Envato Market
+5 more
SwiftIdeas says

^^ Got there before me, that’s the code we use too :)

- Ed

741 posts
  • Had an item featured on Envato Market
  • Won a Most Wanted contest
  • Has been part of the Envato Community for over 5 years
  • Has sold $40,000+ on Envato Market
+4 more
Orbital_Themes says

You really don’t need the whole parse_shortcode_content function as you don’t use it anywhere. The shortcode_unautop function is part of core WordPress filters, but somehow it is not applied or applied before wpautop filter. For shortcodes to work properly you only need the three last lines of your code.

I use it in my themes and it works great. The thing with this piece of code is that some plugins use different methods of cleaning up shortcode content and this method interferes with said plugins. Few of my customers have contacted me about the issue, I then told them to comment out the three lines and let plugins clear the shortcodes their way.

I can see why reviewers would reject code that messes with default WordPress functions and filters, but they just see the remove_filter line, not the whole picture. You are just moving the filter priority around and that will not cause any issues (apart form the one described above, but plugins are to blame for this too).

@kaaz This has been around for as long I can remember and I have no idea why it hasn’t been fixed yet. :(

@RDever Your code looks promising, I’ll give it a try! :)

5823 posts
  • Member of the Envato Team
  • Has been part of the Envato Community for over 4 years
  • Located in Canada
  • Joined us in one of our Envato Live Events
+13 more
KingDog Envato team says

You got to take a bit of a chill pill xstortionist :) You know the forum rules about calling down hate on the reviewers, let’s try and keep things constructive.

122 posts WordPress Wizard
  • Elite Author: Sold more than $75,000 on Envato Market
  • Made it to the Authors' Hall of Fame
  • Had an item featured on Envato Market
  • Won a Most Wanted contest
+5 more
CODE9RS says

Hi @pogoking.

The last 3 lines are the problem hahahaha you can’t (you SHOULDN ’T) remove and move the order of the filters.

This is a core modification, when you just add a filter, it’s fine, but when you remove and change the order, it isn’t…

Themes are the visual part of the WP installation and should be our mission don’t break any plugin… a CSS rule missing or styling issue it’s okay, but totally break the plugin isn’t ok to the users.

Yes, give a try. I’m getting great results with this code :D

Cheers, Rafael Angeline

499 posts
  • Has referred 1000+ members
  • Has sold $250,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Elite Author: Sold more than $75,000 on Envato Market
+8 more
pixelentity says

     add_filter('the_content', 'shortcode_empty_paragraph_fix');

    function shortcode_empty_paragraph_fix($content)
    {   
        $array = array (
            '<p>[' => '[', 
            ']</p>' => ']', 
            ']<br />' => ']'
        );

        $content = strtr($content, $array);

        return $content;
    }
this still gets applied to all shortcodes, including 3rd party ones and alters formatting for “inline” scs. Other authors got rejected by using this code too.
741 posts
  • Had an item featured on Envato Market
  • Won a Most Wanted contest
  • Has been part of the Envato Community for over 5 years
  • Has sold $40,000+ on Envato Market
+4 more
Orbital_Themes says

You’re right, but the only reason why I used to rearrange filters was because all the other code snippets around the internet didn’t work. Your code seems to work great, just have to test if it works in all the cases (e.g. if you put the shortcode in separate line vs on the same line as it’s content but with a space in between). Cheers and thanks for the tip!

EDIT : @pixelenity So what do you suggest? What do you use?

3072 posts
  • Has referred 50+ members
  • Has sold $40,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Had an item featured on Envato Market
+5 more
wickedpixel says

What about js? Search with it the empty paragraphs or br tags in forbidden areas and remove them. Is that allowed? :)

Helpful Information

  • Please read our community guidelines. Self promotion and discussion of piracy is not allowed.
  • Open a support ticket if you would like specific help with your account, deposits or purchases.
  • Item Support by authors is optional and may vary. Please see the Support tab on each item page.

Most of all, enjoy your time here. Thank you for being a valued Envato community member.

Post Reply

Format your entry with some basic HTML. Read the Full Details, or here is a refresher:

<strong></strong> to make things bold
<em></em> to emphasize
<ul><li> or <ol><li> to make lists
<h3> or <h4> to make headings
<pre></pre> for code blocks
<code></code> for a few words of code
<a></a> for links
<img> to paste in an image (it'll need to be hosted somewhere else though)
<blockquote></blockquote> to quote somebody

:grin: :shocked: :cry: Complete List of Smiley Codes

by
by
by
by
by
by