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.