Skip to main content
some more details, a link to a php function, spelling, example HTML code
Source Link
LWS-Mo
  • 1.6k
  • 1
  • 10
  • 16

I created a simple shortcode and added it two times to a page like so:

[my-shortcode]something[/my-shortcode]
[my-shortcode]else[/my-shortcode]

As you can see, I added a line break (<br><br />) after the first shortcode. So in the frontend it is also shown like this:

something
else

And in the HTML:

<span>something</span>
<br />
<span>else</span>

Than I also used the the_content filter but with strtr() like this:

    function remove_br_from_shortcodes($content){
        
        // look for the string "]<br />" and replace it just with "]"
        $replace = array (
            ']<br />' => ']'
        );
        $content = strtr($content, $replace);

        return $content;

    }
    add_filter('the_content', 'remove_br_from_shortcodes');

After refreshing the page, the frontend now shows:

something else

In the HTML:

<span>something</span>
<span>else</span>

As the name of the_content filter already suggests, it is a filter for the normal post/page content, not, for example text-widgets or excerpts.

If you also want to remove paragraphs (<p>) than please try adding this in the $replace array: (watch your comma´s!)

'<p>[' => '[',
']</p>' => ']',

With the PHP strtr() function, you can replace certain characters in a string. More to read here.

I created a simple shortcode and added it two times to a page like so:

[my-shortcode]something[/my-shortcode]
[my-shortcode]else[/my-shortcode]

As you can see, I added a line break (<br>) after the first shortcode. So in the frontend it is also shown like this:

something
else

Than I also used the the_content filter but like this:

    function remove_br_from_shortcodes($content){
        
        // look for the string "]<br />" and replace it just with "]"
        $replace = array (
            ']<br />' => ']'
        );
        $content = strtr($content, $replace);

        return $content;

    }
    add_filter('the_content', 'remove_br_from_shortcodes');

After refreshing the page, the frontend now shows:

something else

As the name of the_content filter already suggests, it is a filter for the normal post/page content, not, for example text-widgets or excerpts.

If you also want to remove paragraphs (<p>) than please try adding this in the $replace array: (watch your comma´s!)

'<p>[' => '[',
']</p>' => ']',

With the PHP strtr() function, you can replace certain characters in a string. More to read here.

I created a simple shortcode and added it two times to a page like so:

[my-shortcode]something[/my-shortcode]
[my-shortcode]else[/my-shortcode]

As you can see, I added a line break (<br />) after the first shortcode. So in the frontend it is also shown like this:

something
else

And in the HTML:

<span>something</span>
<br />
<span>else</span>

Than I also used the the_content filter but with strtr() like this:

    function remove_br_from_shortcodes($content){
        
        // look for the string "]<br />" and replace it just with "]"
        $replace = array (
            ']<br />' => ']'
        );
        $content = strtr($content, $replace);

        return $content;

    }
    add_filter('the_content', 'remove_br_from_shortcodes');

After refreshing the page, the frontend now shows:

something else

In the HTML:

<span>something</span>
<span>else</span>

As the name of the_content filter already suggests, it is a filter for the normal post/page content, not, for example text-widgets or excerpts.

If you also want to remove paragraphs (<p>) than please try adding this in the $replace array: (watch your comma´s!)

'<p>[' => '[',
']</p>' => ']',

With the PHP strtr() function, you can replace certain characters in a string. More to read here.

some more details, a link to a php function, spelling
Source Link
LWS-Mo
  • 1.6k
  • 1
  • 10
  • 16

I created a simple shortcode and added it two times to a page like so:

[my-shortcode]something[/my-shortcode]
[my-shortcode]else[/my-shortcode]

As you can see, I added a line break (<br>) after the first shortcode. So in the frontend it is also shown aslike this:

something
else

Than I also used the the_content filter but like this:

    function remove_br_from_shortcodes($content){
        
        // look for the string "]<br />" and replace it just with "]"
        $replace = array (
            ']<br />' => ']'
        );
        $content = strtr($content, $replace);

        return $content;

    }
    add_filter('the_content', 'remove_br_from_shortcodes');

After refreshing the page, the frontend now shows:

something else

As the name of the_content filter already suggests, it is a filter for the normal post/page content, not, for example text-widgets or excerpts.

If you also want to remove paragraphs (<p>) than please try adding this in the $replace array: (watch your comma´s!)

'<p>[' => '[',
']</p>' => ']',

With the PHP strtr() function, you can replace certain characters in a string. More to read here.

I created a simple shortcode and added it two times to a page like so:

[my-shortcode]something[/my-shortcode]
[my-shortcode]else[/my-shortcode]

As you can see, added a line break (<br>) after the first shortcode. So in the frontend it is also shown as:

something
else

Than I also used the the_content filter but like this:

    function remove_br_from_shortcodes($content){
        
        // look for the string "]<br />" and replace it with "]"
        $replace = array (
            ']<br />' => ']'
        );
        $content = strtr($content, $replace);

        return $content;

    }
    add_filter('the_content', 'remove_br_from_shortcodes');

After refreshing the page, the frontend now shows:

something else

If you also want to remove paragraphs (<p>) than please try adding this in the $replace array:

'<p>[' => '[',
']</p>' => ']',

I created a simple shortcode and added it two times to a page like so:

[my-shortcode]something[/my-shortcode]
[my-shortcode]else[/my-shortcode]

As you can see, I added a line break (<br>) after the first shortcode. So in the frontend it is also shown like this:

something
else

Than I also used the the_content filter but like this:

    function remove_br_from_shortcodes($content){
        
        // look for the string "]<br />" and replace it just with "]"
        $replace = array (
            ']<br />' => ']'
        );
        $content = strtr($content, $replace);

        return $content;

    }
    add_filter('the_content', 'remove_br_from_shortcodes');

After refreshing the page, the frontend now shows:

something else

As the name of the_content filter already suggests, it is a filter for the normal post/page content, not, for example text-widgets or excerpts.

If you also want to remove paragraphs (<p>) than please try adding this in the $replace array: (watch your comma´s!)

'<p>[' => '[',
']</p>' => ']',

With the PHP strtr() function, you can replace certain characters in a string. More to read here.

Source Link
LWS-Mo
  • 1.6k
  • 1
  • 10
  • 16

I created a simple shortcode and added it two times to a page like so:

[my-shortcode]something[/my-shortcode]
[my-shortcode]else[/my-shortcode]

As you can see, added a line break (<br>) after the first shortcode. So in the frontend it is also shown as:

something
else

Than I also used the the_content filter but like this:

    function remove_br_from_shortcodes($content){
        
        // look for the string "]<br />" and replace it with "]"
        $replace = array (
            ']<br />' => ']'
        );
        $content = strtr($content, $replace);

        return $content;

    }
    add_filter('the_content', 'remove_br_from_shortcodes');

After refreshing the page, the frontend now shows:

something else

If you also want to remove paragraphs (<p>) than please try adding this in the $replace array:

'<p>[' => '[',
']</p>' => ']',