Pippins Plugins
  • Email
  • Facebook
  • Feedburner
  • Github
  • Google
  • Twitter
  • Vimeo
  • Youtube
  • Rss
  • About
  • News
  • Join the Site
    • Member Benefits
    • Member Plugins
    • Email Notifications
  • Plugin Store
    • Affiliate Area
    • Checkout
  • Plugins
    • Plugin Portfolio
      • Plugin Portfolio – List View
    • Free
    • Premium
    • Member Plugins
    • Coding Standards
    • Get Plugin Support
  • Tutorials
    • Series
      • Plugin Development 101
      • Creating a User Follow System Plugin
      • Customizing Restrict Content Pro
      • Displaying Content with Easy Content Types
      • Writing Your First WordPress Plugins, Basic to Advanced
      • Working with Widgets
      • User Submitted Image Galleries
      • Plugin Thoughts
      • Integrating Stripe.com with WordPress
      • WordPress Rewrite API
    • Member Exclusive
      • Free Members
      • Subscriber Only
    • Difficulty
      • Beginner
      • Intermediate
      • Advanced
    • Action and Filter Hooks
    • Ajax
    • Custom Post Types
    • External APIs
    • Short Codes
    • Taxonomies
    • Video Tutorials
    • Widget Tutorials
    • WordPress Admin / Dashboard
    • Working with jQuery
    • WordPress Database
    • Writing Plugins
    • Tag Index
  • Reviews
  • Support Forum
  • Contact
    • Support the Site
    • Request Code Review
    • Plugin Support

Never Remove the Default the_content Filters in Themes

Posted on September 24, 2012 by Pippin in Thoughts 15 Comments
Home» Thoughts » Never Remove the Default the_content Filters in Themes
Tweet
Love It - 1

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.

Tweet Follow @pippinsplugins
short codes, the_content

15 comments on “Never Remove the Default the_content Filters in Themes”

  1. turtlepod says:
    September 24, 2012 at 6:34 pm

    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.

    Reply
  2. Tom McFarlin says:
    September 25, 2012 at 5:26 am

    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.

    Reply
    • Pippin says:
      September 25, 2012 at 9:08 am

      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 says:
    September 25, 2012 at 9:28 am

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

    Reply
    • Pippin says:
      September 25, 2012 at 9:42 am

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

  4. Steven Gliebe says:
    September 25, 2012 at 10:05 am

    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.

    Reply
    • Pippin says:
      September 25, 2012 at 10:14 am

      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 says:
    September 25, 2012 at 12:28 pm

    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);

    Reply
    • Pippin says:
      September 25, 2012 at 12:57 pm

      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 says:
      September 25, 2012 at 1:14 pm

      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 says:
      September 25, 2012 at 1:17 pm

      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 says:
      September 29, 2012 at 10:33 pm

      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 says:
      September 30, 2012 at 12:55 pm

      WP-Markdown is awesome.

  6. jimbob says:
    November 1, 2012 at 10:57 am

    hi im using this theme

    http://themeforest.net/item/wp-diggity-pro-layout-builder-theme/full_screen_preview/3220226

    and want to use revolution slider but i get a grey box wherever i put it

    is there a way that i can fix it myself -it is not too clear in the video for someone like me.

    cheers
    jimbob

    Reply
    • Pippin says:
      November 1, 2012 at 7:46 pm

      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.

Leave a Reply Cancel reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

  • Login

Lost your password?

Please enter your username or e-mail address. You will receive a new password via e-mail.

  • Facebook Become a Fan Like

  • Twitter Subscribe on Twitter Follow

  • YouTube Follow my Videos Subscribe

  • RSS Feed Subscribe with RSS Subscribe

Easy Digital Downloads

Most Loved

  • Love It Pro for WordPress
  • Write a “Love It” Plugin with Ajax to Let Users Love Their Favorite Posts / Pages
  • Simple Notices Pro Plugin for WordPress
  • User Bookmarks for WordPress
  • Front End Registration and Login Forms Plugin

Similar Plugins and Posts

  • Playing Nice with the “the_content” Filter
  • User Submitted Image Gallery – Part 6
  • Writing Your First WordPress Plugin Part 3
  • Load Scripts if Post has Short Code
  • User Friendly Short Codes Plugin Example

Latest Premium Content

  • Plugin Development 101 – Introduction to Adding Dashboard Menus
  • Plugin Development 101 – Intro to Loading Scripts and Styles
  • User Follow System – Part 5
  • Plugin Development 101 – Intro to Short Codes
  • Plugin Development 101 – Registering a Custom Post Type
  • Plugin Development 101 – Intro to Actions

Latest Tutorials

  • Submitting Your First Pull Request to a WordPress Plugin on Github (2)

    Github is an extremely popular tool for managing WordPress plugins, and one...

  • Plugin Development 101 – Introduction to Adding Dashboard Menus (1)

    Adding new menus, both top level and sub level, to the WordPress Dashboard is a really common task for plugins...

  • Plugin Development 101 – Intro to Loading Scripts and Styles (16)

    In this part of Plugin...

Enter your email to receive automated updates when new posts are published

Latest Tweets

  • RT @WordCampSF: WordCamp SF tickets now on sale. Get &#039;em while they&#039;re hot! http://t.co/Tet8mS0TlT #WCSF
    May 23, 2013
  • @jasonhobbsllc Sure. 1pm CST?
    May 23, 2013
  • WordPress Plugin Prices Are Too Low http://t.co/MmgH44WpXt via @chrislema
    May 23, 2013

Topics

contextual help featured get_user_meta attachments campaign monitor register_setting wp_enqueue_script hook Rémi Corson the_content shortcodes Sugar Event Calendar add_shortcode mail chimp plugin authors attachment image forms login short codes do_action Related posts comments recent posts post types apply_filters bbpress short code taxonomies custom post type images gallery Ajax Stripe taxonomy jquery users widgets add_filter easy content types add_action widget restrict content pro easy digital downloads

Weekly Newsletter

Useful Links

  • Join the Site
  • Plugin Store
  • Affiliate Area
  • Tag Index
  • Support the Site
  • Suggest a Tutorial
  • Random Post
  • Contact

Monthly Archives

(c) 2013 Pippin's Plugins