There is a terrible, terrible practice among theme developers to remove some of the default filters that are applied to post and page content. The goal in removing these filters is to fix formatting problems with short codes, primarily column short codes. If you have ever done this, or would like to know why you should never do it, watch my video demonstration of the problem it causes below.

This technique came around because of one (just one!) tutorial that was published in 2012. This tutorial has, unfortunately, resulted in this technique being used in thousands of themes, particularly commercial themes.

The function that causes all of the problems looks like this (or some similar form):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
function webtreats_formatter($content) {
	$new_content = '';
 
	/* Matches the contents and the open and closing tags */
	$pattern_full = '{(\[raw\].*?\[/raw\])}is';
 
	/* Matches just the contents */
	$pattern_contents = '{\[raw\](.*?)\[/raw\]}is';
 
	/* Divide content into pieces */
	$pieces = preg_split($pattern_full, $content, -1, PREG_SPLIT_DELIM_CAPTURE);
 
	/* Loop over pieces */
	foreach ($pieces as $piece) {
		/* Look for presence of the shortcode */
		if (preg_match($pattern_contents, $piece, $matches)) {
 
			/* Append to content (no formatting) */
			$new_content .= $matches[1];
		} else {
 
			/* Format and append to content */
			$new_content .= wptexturize(wpautop($piece));
		}
	}
 
	return $new_content;
}
 
// Remove the 2 main auto-formatters
remove_filter('the_content', 'wpautop');
remove_filter('the_content', 'wptexturize');
 
// Before displaying for viewing, apply this function
add_filter('the_content', 'webtreats_formatter', 99);
add_filter('widget_text', 'webtreats_formatter', 99);

Please, please, stop using this. The problem it causes has literally resulted in dozens of hours of support for me and many other plugin developers.

  1. turtlepod

    the source comes from this topic in wp.org, (3 years ago, the plugin files in no longer available.)
    http://wordpress.org/support/topic/plugin-remove-wpautop-wptexturize-with-a-shortcode

    moderator note: Using a raw shortcode or shortcodes that remove auto formatting will cause plugin incompatibility and prevent you from using any other shortcodes. http://theandystratton.com/2011/shortcode-autoformatting-html-with-paragraphs-and-line-breaks for more information.

    • Rca Ieftin Craiova

      This is in very isolated cases, but I’ve seen nothing but trouble when people much with wpautop and literally removing the_content filters.

  2. Tom McFarlin

    Whenever I’ve been working on a plugin or a feature and I need to remove a paragraph wrapper, I hook into the_content then use a regular expression to parse out the text and return it.

    This is in very isolated cases, but I’ve seen nothing but trouble when people much with wpautop and literally removing the_content filters.

    • Pippin

      Right, it’s totally acceptable to do in special situations that call for it, but it’s never okay to do globally.

  3. Cliff Seal

    Does moving the priority of wpautop to 11 cause the same (or similar) issues in your experience?

    • Pippin

      No, it shouldn’t, at least not for any reason I can think of.

  4. Steven Gliebe

    Just a note to viewers to say that the theme in this video was updated earlier with a fix for the issue.

    Thank you again for bringing attention to this bad practice. I think this time people are listening or at least the ThemeForest staff is and that should go a long way to make life easier for plugin developers and customers.

    BTW, the tutorial you mentioned is actually from way back in 2010 and I think it would be worth everybody reading this posting a comment and asking the author to remove or update the tutorial.

    • Pippin

      Thanks Steven, I had meant to make a note of that after you pointed it out to my last night.

      Everyone else: Steven has done exactly what he should have here. He realized that the code was bad and so removed it right away. There is nothing inherently wrong with using this technique (unless doing so blindly) because it does, at first glance, appear to really help, so I’m not trying to point fingers at anyone or make light of anyone else’s hard work. I’m thrilled that Steven immediately informed me that he had updated the theme because that’s what should happen in these scenarios.

  5. Jason Bobich

    I am definitely guilty of having this in a theme (it’s even in the theme of your site you’re using right now with some slight modifications to avoid the issues you’re describing in your video) — However, I have now moved to a plugin where it can be disabled. And so I’m not saying this kind of functionality should ever be in a theme, but I also think you’re being a little harsh on the snippet in general.

    The entire issue you’re describing with the code you’re copying above can easily be fixed by the priority in which it’s added. The problem you’re describing specifically in your video arises is because of the fact that the wpauto filter gets put back at a priority of 99, which is AFTER shortcodes have been rendered.

    With WordPress’s the_content, wpautop happens before do_shortcode — And so the whole mess you’re describing in your example is because this has now been flip-flopped.

    So, in your snippet above, I believe your entire problem could be fixed by changing this line:

    add_filter('the_content', 'webtreats_formatter', 99);

    To this:

    add_filter('the_content', 'webtreats_formatter', 9);

    • Pippin

      A bit harsh? Most likely, but I can tell you that I have literally lost hours and hours of time due to support requests because of these snippets.

      As Tom pointed out, there are definitely some (very few) valid instances for using a filter like this, but in general it shouldn’t be in a theme in the first place, particularly since short codes should be in a plugin to begin with. I’m thrilled you’re moving towards the plugin route :D

      Now, in terms in fixing the issue via priority, yes, you’re right, changing the priority to 9 (one lower that default) does indeed fix the issue shown in the video, but I am doubtful that it would fix these issues across the board. It fixes the webtreats_formatter version, though I don’t believe the priority would fix the version shown in the video (from Steven’s theme) since that uses a different method.

      When it comes down to it, ultimately snippets like this simply shouldn’t exist in themes at all, even if they are done right. They adjust and mess with default WordPress formatting and behavior that many, many plugins rely on. While it may not always break, the likely hood of breaking something by adding this kind of filter is significantly higher.

    • Jason Bobich

      I understand your beef is more with people just throwing stuff this into their themes in general. Or throwing stuff like this into a plugin when they truly don’t understand what it’s doing.

      I guess the reason I say a bit harsh on that particular snippet is (with the exception of the priority issue) it actually does solve a pretty tough dilemma. —

      If you created a true “raw” shortcode, with add_shortcode, that held content within you didn’t want to format, by the time do_shortcode happens, WordPress has already filled it with auto formatting. And if do_shortcode were to happen before the auto formatting, well then the entire issue you ranted about in your video would happen. So how on earth can you still have the basic formatting of WP posts but also have a [raw] shortcode that allows you to escape it when you want? And I think this snippet (whoever originally created it) is overall a pretty genius solution to that.

      In my personal sites, I use my version of the raw shortcode so often when I’m writing tutorials and what not. I honestly can’t imagine writing my pages and posts without this raw shortcode. Maybe I just suck at using WordPress as an end-user, but so often I want to use a couple different shortcodes in conjunction or near each other, but even more often, I want to copy and paste HTML code in my post or page. WP auto formatting always messes these things up.

    • Pippin

      I think we’re talking about two different things.

      [raw] short codes are fine and perfect. They fix the issue perfectly (I use the one you added to this theme). I have no problem with raw short codes at all because the effect the formatting in a localized area.

      The problem I have is with functions like the webtreats one that are global: they literally effect ALL content, not just the content inside of [raw]…[/raw]

    • Ads

      In my personal sites, I use my version of the raw shortcode so often when I’m writing tutorials and what not. I honestly can’t imagine writing my pages and posts without this raw shortcode. Maybe I just suck at using WordPress as an end-user, but so often I want to use a couple different shortcodes in conjunction or near each other, but even more often, I want to copy and paste HTML code in my post or page. WP auto formatting always messes these things up.

      Its for this very reason why I use Markdown to write my posts which are almost all tutorials with numerous snippets of code and other fancy sh%t.

      In fact in all my years developing for and using WP I can’t say I’ve ever used a short code in a post other than writing them for clients or as this article describes, fixing them – but that’s just me – because sure I agree shortcodes have their uses, sometimes.

      WP-Markdown by Stephen Harris is what you need.

      With in-post live preview as you type, this little plugin allows me to smash out content quicker than the WP initialization process … well just plain quick, ya get me.

      This implementation of Markdown in WP is the best one to-date, which closely mimics the experience you have on a site like WPSE.

      I recommend checking it out…

    • Pippin

      WP-Markdown is awesome.

    • Pippin

      I’m sorry but I don’t see how that is relevant to this post at all. Please contact the theme developer about the issue.

Your email address will not be published. Required fields are marked *