2178 posts
  • Elite Author
  • Sold between 250 000 and 1 000 000 dollars
  • Exclusive Author
  • Community Moderator
  • Bought between 50 and 99 items
  • Referred between 200 and 499 users
  • Has been a member for 5-6 years
  • Won a Competition
+10 more
mpc Volunteer moderator 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?

369 posts
  • Envato Studio (Microlancer) Beta Tester
  • Sold between 10 000 and 50 000 dollars
  • Most Wanted Bounty Winner
  • Bought between 10 and 49 items
  • Exclusive Author
  • Has been a member for 3-4 years
kaaz says

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

115 posts WordPress Wizard
  • Elite Author
  • Sold between 100 000 and 250 000 dollars
  • Most Wanted Bounty Winner
  • Exclusive Author
  • Envato Studio (Microlancer) Beta Tester
  • Has been a member for 3-4 years
  • Bought between 10 and 49 items
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
  • Power Elite Author
  • Power Elite Author: Sold between 1 000 000 - 1 999 999 dollars
  • Bought between 10 and 49 items
  • Referred between 200 and 499 users
  • United Kingdom
  • Has been a member for 3-4 years
  • Exclusive Author
SwiftIdeas says

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

- Ed

741 posts
  • Has been a member for 5-6 years
  • Sold between 50 000 and 100 000 dollars
  • Exclusive Author
  • Most Wanted Bounty Winner
  • Bought between 50 and 99 items
  • Poland
  • Referred between 10 and 49 users
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! :)

5222 posts
  • Envato Staff
  • Has attended an Envato Live event
  • Support Staff
  • Has been a member for 4-5 years
  • Attended a Community Meetup
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Bought between 50 and 99 items
  • Canada
  • Community Ambassador
+9 more
KingDog Staff 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.

115 posts WordPress Wizard
  • Elite Author
  • Sold between 100 000 and 250 000 dollars
  • Most Wanted Bounty Winner
  • Exclusive Author
  • Envato Studio (Microlancer) Beta Tester
  • Has been a member for 3-4 years
  • Bought between 10 and 49 items
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

491 posts
  • Elite Author
  • Sold between 250 000 and 1 000 000 dollars
  • Won a Competition
  • Referred between 1000 and 1999 users
  • Author had a Free File of the Month
  • Author had a File in an Envato Bundle
  • Bought between 10 and 49 items
+3 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
  • Has been a member for 5-6 years
  • Sold between 50 000 and 100 000 dollars
  • Exclusive Author
  • Most Wanted Bounty Winner
  • Bought between 50 and 99 items
  • Poland
  • Referred between 10 and 49 users
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 been a member for 6-7 years
  • Won a Competition
  • Sold between 50 000 and 100 000 dollars
  • Bought between 10 and 49 items
  • Referred between 50 and 99 users
  • Exclusive Author
  • Romania
wickedpixel says

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

by
by
by
by
by
by