ThemeForest

Shortcode paragraph removal

91 posts
  • Elite Author
  • Sold between 250 000 and 1 000 000 dollars
  • Beta Tester
  • Referred between 500 and 999 users
  • Bought between 50 and 99 items
  • Has been a member for 4-5 years
  • Exclusive Author
elemis says

Hello guys,

Is there a way of only removing p tags that are generated inside shortcodes? We don’t want to use wpautop filter. The clients should still be able to format their text by line breaks. But when this is being applied in shortcodes it messes up shortcode output.

We reeeeeally reaaaally need to solve this :)

Any help would be appreciated. Thanks in advance.

238 posts
  • Bosnia and Herzegovina
  • Bought between 1 and 9 items
  • Exclusive Author
  • Has been a member for 3-4 years
ewizz says

p tags in shortcodes?

91 posts
  • Elite Author
  • Sold between 250 000 and 1 000 000 dollars
  • Beta Tester
  • Referred between 500 and 999 users
  • Bought between 50 and 99 items
  • Has been a member for 4-5 years
  • Exclusive Author
elemis says

p tags in shortcodes?

When writing posts with shortcodes

 [divtag]
 abc
 [/divtag]

ends up as

< div >
<p>abc</p>
<p>< / div ></p>

We want to get rid of this automatically generated p tags.

StefanoGiliberti
StefanoGiliberti Recent Posts
Threads Started
32 posts
  • Bought between 10 and 49 items
  • Exclusive Author
  • Has been a member for 4-5 years
  • Referred between 10 and 49 users
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Won a Competition
StefanoGiliberti says
function formatter($content) {
    $new_content = '';
    $pattern_full = '{(\[raw\].*?\[/raw\])}is';
    $pattern_contents = '{\[raw\](.*?)\[/raw\]}is';
    $pieces = preg_split($pattern_full, $content, -1, PREG_SPLIT_DELIM_CAPTURE);
    foreach ($pieces as $piece) {
        if (preg_match($pattern_contents, $piece, $matches)) {
            $new_content .= $matches[1];
        } else {
            $new_content .= wptexturize(wpautop($piece));
        }
    }
    return $new_content;
}
remove_filter('the_content', 'wpautop');
remove_filter('the_content', 'wptexturize');
add_filter('the_content', 'formatter', 99);

[raw]Content without Paragraphs[/raw]

91 posts
  • Elite Author
  • Sold between 250 000 and 1 000 000 dollars
  • Beta Tester
  • Referred between 500 and 999 users
  • Bought between 50 and 99 items
  • Has been a member for 4-5 years
  • Exclusive Author
elemis says

function formatter($content) {
    $new_content = '';
    $pattern_full = '{(\[raw\].*?\[/raw\])}is';
    $pattern_contents = '{\[raw\](.*?)\[/raw\]}is';
    $pieces = preg_split($pattern_full, $content, -1, PREG_SPLIT_DELIM_CAPTURE);
    foreach ($pieces as $piece) {
        if (preg_match($pattern_contents, $piece, $matches)) {
            $new_content .= $matches[1];
        } else {
            $new_content .= wptexturize(wpautop($piece));
        }
    }
    return $new_content;
}
remove_filter('the_content', 'wpautop');
remove_filter('the_content', 'wptexturize');
add_filter('the_content', 'formatter', 99);

[raw]Content without Paragraphs[/raw]

Thanks you for your support. However surrounding shortcodes with another shortcode probably will be too complex for some users :(

556 posts You Reap What You Saw
  • Author had a Free File of the Month
  • Bought between 1 and 9 items
  • Elite Author
  • Exclusive Author
  • Has been a member for 3-4 years
  • Referred between 200 and 499 users
  • Sold between 50 000 and 100 000 dollars
Bluz says


function formatter($content) {
    $new_content = '';
    $pattern_full = '{(\[raw\].*?\[/raw\])}is';
    $pattern_contents = '{\[raw\](.*?)\[/raw\]}is';
    $pieces = preg_split($pattern_full, $content, -1, PREG_SPLIT_DELIM_CAPTURE);
    foreach ($pieces as $piece) {
        if (preg_match($pattern_contents, $piece, $matches)) {
            $new_content .= $matches[1];
        } else {
            $new_content .= wptexturize(wpautop($piece));
        }
    }
    return $new_content;
}
remove_filter('the_content', 'wpautop');
remove_filter('the_content', 'wptexturize');
add_filter('the_content', 'formatter', 99);

[raw]Content without Paragraphs[/raw]

Thanks you for your support. However surrounding shortcodes with another shortcode probably will be too complex for some users :(

I don’t think so. I use the same technique and so far no complaints from clients.

1382 posts
  • Bought between 10 and 49 items
  • Exclusive Author
  • Has been a member for 3-4 years
  • Referred between 10 and 49 users
  • Serbia
wpCanyonThemes says

Or in the shortcodes you want to do this for, just before you return the output put [raw] before the output string and [/raw] after it. Then return do_shortcode($output) and that’s it :)

So the user doesn’t have to do it himself.

238 posts
  • Bosnia and Herzegovina
  • Bought between 1 and 9 items
  • Exclusive Author
  • Has been a member for 3-4 years
ewizz says

Or in the shortcodes you want to do this for, just before you return the output put [raw] before the output string and [/raw] after it. Then return do_shortcode($output) and that’s it :) So the user doesn’t have to do it himself.

man! where have you been? you didn’t posted anything on forum for whole day !! :D

1382 posts
  • Bought between 10 and 49 items
  • Exclusive Author
  • Has been a member for 3-4 years
  • Referred between 10 and 49 users
  • Serbia
wpCanyonThemes says

Noone needed my help whole day :)

And finishing up the theme so don’t have much time for the forum :)

@winterbits – Any progress?

658 posts
  • Sold between 5 000 and 10 000 dollars
  • Referred between 10 and 49 users
  • Bought between 10 and 49 items
  • Has been a member for 4-5 years
  • Exclusive Author
  • Microlancer Beta Tester
ChillThemes says

Put this in functions.php.

function remove_wpautop($content) { 
    $content = do_shortcode( shortcode_unautop($content) ); 
    $content = preg_replace( '#^<\/p>|^<br \/>|<p>$#', '', $content );
    return $content;
}
</p>

Example/Usage:

add_shortcode( 'blockquote', 'blockquote_shortcode' );
function blockquote_shortcode( $attr, $content = null ) {
    $return .= '<blockquote>';
    $return .= remove_wpautop($content);
    $return .= '</blockquote>';
    return $return;
}
by
by
by
by
by