Stop WordPress automatically adding tags to post content

The answer by shea is not ideal as in many cases:

  • You don’t want to strip everything from <br>, <p> etc. You want it as a default behavior for your WP visual composer which the above code will delete
  • In many cases it is considered as “hacking the core” as this is changing the
    default core behavior of WP – for example such a thing will not pass
    on ThemeForest

As I can see you mainly have issues with you shortcodes. The right way to approach this is not to change the default behavior (hack the core) but to just filter the content. So just add a filter and in a variable pass an array of your shotrcodes you want to filter like this:

function the_content_filter($content) {
    $block = join("|",array("one_third", "team_member"));
    $rep = preg_replace("/(<p>)?\[($block)(\s[^\]]+)?\](<\/p>|<br \/>)?/","[$2$3]",$content);
    $rep = preg_replace("/(<p>)?\[\/($block)](<\/p>|<br \/>)?/","[/$2]",$rep);
return $rep;
}
add_filter("the_content", "the_content_filter");

The content inside will be filtered and therefore your shortcodes will be free of <br>, <p> etc. but the other parts of content – for example standard text in the WP editor created by user – will still have full functionality of WP.

References:

  1. the_content WP filter
  2. Regex “translator”
  3. join PHP function
  4. preg_replace PHP function

Leave a Comment