Shortcodes and wpautop()


#1

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( '

' ), '', $content ); $content = str_replace( array( '

' ), '', $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 :open_mouth: So my question is there any other solution to avoid those empty p and br tags? What do you think about this?


#2

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


#3

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 (
            '

[' => '[', ']

' => ']', ']
' => ']' ); $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 :slight_smile:


#4

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

  • Ed

#5

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. :frowning:

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


#6

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


#7

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 :smiley:

Cheers,
Rafael Angeline


#8
RDever said
 	add_filter('the_content', 'shortcode_empty_paragraph_fix');

	function shortcode_empty_paragraph_fix($content)
    {   
        $array = array (
            '

[' => '[', ']

' => ']', ']
' => ']' ); $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.

#9

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?


#10

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


#11
pogoking said

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

http://themeforest.net/forums/thread/empty-p-tags-when-using-shortcodes/48520?page=3#647866

#12

“ThemeForest theme developers in particular seem to love adding this code to their themes.” - lol


#13

Thanks guys I have fixed it on my own :stuck_out_tongue_winking_eye: But for the next theme I will probably use the provided snippet.


#14

Would be nice to provide the fix since you started the thread and others may be intersted :d


#15
ZoomIt said

Would be nice to provide the fix since you started the thread and others may be intersted :d

+1 for this and up we go :slight_smile:


#16

I had a very strange issue with empty paragraphs and spaces in the the shortcode of my last plugin.

I tried a lot of solution posted by authors here in the past months, but the only thing that solved it was this piece of code:

$data = shortcode content ..
$data= str_replace(array("\r", "
", "\t", "\v"), '', $data);
return $data;

I don’t know if is the best way, but it works :smiley:


#17

I’m also looking for a bulletproof solution, so if anyone has it, please share it with us :slight_smile:


#18

Bulletproof solution: do not any of this. This kind of behavior should only ever be done in plugins.

These kind of snippets literally cause me and many other plugin developers dozens or hundreds of hours in support requests.


#19
mordauk said

Bulletproof solution: do not any of this. This kind of behavior should only ever be done in plugins.

These kind of snippets literally cause me and many other plugin developers dozens or hundreds of hours in support requests.

Indeed. Probably my issue was caused by one of them, since on the Twenty Eleven theme everything was fine.


#20
mordauk said

Bulletproof solution: do not any of this. This kind of behavior should only ever be done in plugins.

These kind of snippets literally cause me and many other plugin developers dozens or hundreds of hours in support requests.

It’s true, in most cases these snippets can cause major problems, however without removing those empty p tags the shortcodes won’t work properly, because WordPress adds them all over the place.

So, again, has anyone found a proper way to create shortcodes in a theme, without the use of a plugin, and without affecting other plugins ?