Empty P Tags When Using Shortcodes


#1

I love the theme I’m working with right now but the shortcodes are leaving behind empty paragraph tags thus messing up spacing. Probably old news but it took me a bit of Googling…this plugin works a treat if you’re having this issue :slight_smile:

http://www.johannheyne.de/wordpress/shortcode-empty-paragraph-fix/


#2

it happens when your shortcode starts or it goes on multiple lines, all that plugin do is disabling wpautop (i guess)


#3

You can also strip formatting by wrapping it in a shortcode like this.


#4

Better to add a code where unwanted p, br and such are removed automatically from the shortcodes only, so no need for a double shortcode and no need for a wp auto p removal which will lead to unwanted formatting errors at well.

That’s just how I do it.


#5

https://gist.github.com/1135818

here is an example also


#6

Thanks for replies folks. Do you mean I would be better off hard coding something in the functions.php file for each shortcode? I’m confused…


#7
Thanks for replies folks. Do you mean I would be better off hard coding something in the functions.php file for each shortcode? I’m confused…
Yeah,

Your theme has wonky poorly written shortcode.

You’ll want something like…

$content = preg_replace(’#^</p>|

$#’, ‘’, $content);

I think that is the correct code. I honestly haven’t written or used shortcode in over a year so I had to dig back into the archives.

The preg_replace is what your missing. It automatically removes the P’s from the shortcode without messing with core wp stuff.

Here is a complete shortcode to put the above code into context.

function clone_box_basic( $atts, $content = null ) { $content = preg_replace('#^<\/p>|

$#', '', $content); return '

' . do_shortcode($content) . '
'; } add_shortcode('box_basic', 'clone_box_basic');

#8
christopherjon said
Thanks for replies folks. Do you mean I would be better off hard coding something in the functions.php file for each shortcode? I’m confused…
Yeah,

Your theme has wonky poorly written shortcode.

You’ll want something like…

$content = preg_replace(’#^</p>|

$#’, ‘’, $content);

I think that is the correct code. I honestly haven’t written or used shortcode in over a year so I had to dig back into the archives.

The preg_replace is what your missing. It automatically removes the P’s from the shortcode without messing with core wp stuff.

Here is a complete shortcode to put the above code into context.

function clone_box_basic( $atts, $content = null ) { $content = preg_replace('#^<\/p>|

$#', '', $content); return '

' . do_shortcode($content) . '
'; } add_shortcode('box_basic', 'clone_box_basic');

Ok, thanks! I thought I was on the right track with preg_replace from what I read at a few sites…that gives me a direction to go. I’d love to fix this myself if at all possible. Enough info to be dangerous anyway :wink:

BTW - how does one nicely refer the author to this thread? Without calling his shortcodes wonky :smiley:


#9
BTW – how does one nicely refer the author to this thread? Without calling his shortcodes wonky
Functionally handicapped may be more politically correct.

#10

you’ll want to use something like

//Clean Up WordPress Shortcode Formatting - important for nested shortcodes
//adjusted from http://donalmacarthur.com/articles/cleaning-up-wordpress-shortcode-formatting/
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 );

#11

Old post, but in case it helps anyone else, I’m just using the php function strip_tags.

strip_tags($content)

This only works if don’t need html formatting within your shortcodes.


#12

strip_tags - This function tries to return a string with all NUL bytes, HTML and PHP tags stripped from a given str.

If you have a onethird par example shortcode any anchors, bolds, headings or whatever inside will be removed. So this is not a solution.


#13
OrganicBeeMedia said

you’ll want to use something like

//Clean Up WordPress Shortcode Formatting - important for nested shortcodes
//adjusted from http://donalmacarthur.com/articles/cleaning-up-wordpress-shortcode-formatting/
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 );

Sorry for the push of this old post.

This code works fine. BUT I also use shortcodes with text areas in the options menu (option tree).

e.g. in the index.php:
do_shortcode(ot_get_option( ‘fp_textbox’ ));

How can I also remove the empty p-Tags there?


#14

Try to return the shortcode with [raw] like this:

function my_shortcode($atts, $content = null){
//your code

   return '[raw]'.$your_output.'[/raw]';
}

#15

thanks. found the same thing here: http://www.wprecipes.com/disable-wordpress-automatic-formatting-on-posts-using-a-shortcode

But If I use RAW it just types [raw] in the text.

return '[raw]
[/raw]'. do_shortcode($content) .'[raw]
[/raw]';

#16

Complete shortcode function, put this into function.php:

function my_shortcode($atts, $content = null){
   //your code

   return '[raw]'.$your_output.'[/raw]';
}
add_shortcode("mysc", "my_shortcode");

After that you’ll be able to use [mysc] shortcode in post/page editor.


#17

I’ve done it exactly like that.
Here’s the whole code:

function lld_one_third( $atts, $content = null ) {
	extract( shortcode_atts( array(
		'align' => ''
	), $atts));
	return '[raw]
[/raw]'. do_shortcode($content) .'[raw]
[/raw]'; } add_shortcode("one_third", "lld_one_third");

And I’ve implemented the code from the link in the functions.php (also tried in the same shortcodes.php). But it still types the [raw] as text


#18
cudazi said

You can also strip formatting by wrapping it in a shortcode like this.

No, do not ever do that. That is one of the worst practices that has plagued WordPress theme development. Covered it a lot over here: http://themeforest.net/forums/thread/wp-theme-forward-compatibility-shortcodescpts/75050


#19

The RAW tag works for the shortcodes used in a post or page

But I also use Shortcodes when using Theme Options, e.g.
do_shortcode(ot_get_option( ‘fp_textbox’ ));

And then it just types [raw] Texttexttext [/raw]

Do you have a solution for that?


#20

Do I have to change “the_content” to something else to make it work?

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