Skip to content

Instantly share code, notes, and snippets.

@bitfade
Last active January 21, 2022 03:06
Show Gist options
  • Save bitfade/4555047 to your computer and use it in GitHub Desktop.
Save bitfade/4555047 to your computer and use it in GitHub Desktop.
WordPress - Remove empty p tags for custom shortcodes
<?php
add_filter("the_content", "the_content_filter");
function the_content_filter($content) {
// array of custom shortcodes requiring the fix
$block = join("|",array("col","shortcode2","shortcode3"));
// opening tag
$rep = preg_replace("/(<p>)?\[($block)(\s[^\]]+)?\](<\/p>|<br \/>)?/","[$2$3]",$content);
// closing tag
$rep = preg_replace("/(<p>)?\[\/($block)](<\/p>|<br \/>)?/","[/$2]",$rep);
return $rep;
}
@cecowe
Copy link

cecowe commented Jul 10, 2018

Hey,
I have a child theme active right now. Should I add this snippet to functions of the child theme or the parent theme?
How may I add the code, in the end of the .php or?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment