WordPress Bits

Hacking WordPress. Keeping the bits together.

A look at WordPress filters. Disable wpautop() on post formatting.

Posted by Leonid Mamchenkov on August 6, 2007

WordPress provides a number of ways to control how content is stored and displayed. There are themes, which I’m sure everybody has already heard of. There are widgets, which are picking up the fame. But there are things deeper in, which aren’t as famous. They are called filters. And WordPress has quite a few of them.

There are some default filters in WordPress, which work behind the scenes, formatting the content the best possible way. The thing is though, while this formatting suits most users out there, sometimes you want to have control over how things are done. In this post, we’ll see how to gain that control.

First of all, what is a filter? WordPress filter is a type of hook (another type is action hook, which we briefly look at while writing first WordPress plugin). Filters allow data modification on-the-fly. Which means you can change something after it was read and compiled from the database, but before it is shown to your visitor. Or, you can modify something a visitor sent to your database, before it is actually written there.

Modifying a filter call is as easy as calling add_filter(‘filter_name’,’your_function_name’). With the only caveat being that you need to know what data WordPress sends to a filter function. But, as always, there are a few ways to find out.

A good way to start playing with filters is checking out what defaults are and how they work. Default filters are listed in wp-includes/default-filters.php file. From this list, you can learn the names of some filters, the names of functions which are called, and the order in which they are called.

Once you know the names of functions which WordPress uses as filters, you can find them in the rest of PHP files in wp-includes/ directory, see which parameters they accept, what values they return, and how they work in general. This is one of the beauties of the Open Source Software, which WordPress is a great example of.

Let’s get to a practical example, to see how it all works. In your theme, which is the function call which produces the most content? Well, while WordPress can be used for a number of things, I’d say it’s safe to assume that the_content() line which displays the content of your posts and pages brings in the most. Let’s dive into WordPress source code and see how it works. It is defined in the file wp-includes/post-template.php around line 53 (WordPress 2.2.2 or so). Here is how it looks:

Source code of the_content() function

(I cut it off a bit on the right, but it doesn’t matter for our example.)

The function is small and easy to understand. First it gets teh content of the post. Then it applies the ‘the_content’ filter. Then it does some more string replacements. And then it prints out the result.

Now, look at the line where it applies the filters. The filter name is ‘the_content’ and argument given to is the actual content of the post. It expects the processed and modified content back, as it will still need to print it.

How is the content modified? Let’s see. We go back to the wp-includes/default-filters.php file and find all lines for the ‘the_content’ filter. There are four of them, calling wptexturize(), convert_smilies(), convert_chars(), and wpautop(). All of these are defined in wp-includes/formatting.php .

Now we have the knowledge of how WordPress processes the content before displaying it visitors of the web site. We can also understand how many formatting plugins work, as modifying filters it the most common way of tweaking content display. And, of course, we can make our own changes.

Just for the sake of the example, let’s disable WordPress’ default wpautop() call for ‘the_content’ filter. Although it does a good job, we know better how our content should look. Even if for the sake of the example.

To disable a filter, we could either comment an appropriate line in wp-includes/default-filters.php – an ugly solution and a big “No!No!”, or write our own plugin – a much better and preferred solution, assuming no other plugin for this functionality exists. Like WP Unformatted from Alex King (which you should definitely check). Here is all that I had to write for the wpautop() to disappear from my content filtering process.

Disable wpautop() plugin

I can’t hide any longer. Yes, you are right. All this post was to write a single line. :) But I hope you’ve learned a thing or two on the way.

About these ads

32 Responses to “A look at WordPress filters. Disable wpautop() on post formatting.”

  1. Jonathan said

    What editor do you use?

  2. Vim, which is a modern variation of the vi classic. ;)

  3. Jonathan said

    Thought so. Didn’t want to accuse you of Emacs without evidence. :o)

  4. “…accuse you of Emacs…” LOL! Sounds like something out of a Law & Order ep…

    “We’re holding him downtown on a 603, Suspicion of Emacs with intent to code, his keyboard’s in impound”

    No worries Leo, I’ll bail ya out anytime, I use BBEdit on the Mac and completely GET the text-editor luv-thang!

  5. Good discussion of filters and timely! THANKS FOR DOING THIS!! I just hammered through custom templates and it’s SOOOO worth it.

  6. […] A look at WordPress filters. Disable wpautop() on post formatting. WordPress provides a number of ways to control how content is stored and displayed. There are themes, which I’m […] […]

  7. Hehe … I don’t even know how this all almost turned into an editor fight ;)

  8. nikan said

    Leonid,

    In plugins, we often see filters that add bits and pieces under each post (buttons of bookmarking is a good example) but which we would not necesarily want to see coming out in the feed too. How can we prevent this happening?
    I tried remove_filter(‘the_content_rss’, ‘myfunc’) but is does nothing of the sort.

  9. Nikan,

    One way to find out would be to disable all plugins that you have activated, and disable all default filters, related to feeds/RSS, and then enable them one by one to spot which one actually adds the functionality that you are trying to get rid off. Once you know what exactly causes the problem, getting rid of it would be as easy as 1-2-and-maybe-3. :)

  10. Jerry said

    Hi, could you tell me what’s the difference between add_filter() & add_action()? I’m confused by these two funcs! Thanks.

  11. Jerry,

    WordPress provides two types of hooks – action and filter. Actions are like event triggers. For example, when all WordPress files are parsed and loaded, but nothing is printed out yet, an action ‘init’ is called. This lets you insert your (or modify) functionality at specific points in WP execution.

    Filters, on the other hand, allow you to modify data. Usually, are given a piece of data (like post’s content before it is saved to the database), and you are expected to return it back, once you are finished processing it with your own functions.

    Is it any clearer now? :)

  12. Jerry said

    Yeah, I get the difference now, thanks.
    Appreciate your speed for reply:)

  13. Binny V A said

    Please provide the text of that plugin – I am too lazy to type it out.

  14. Binny V A said

    The text of the plugin – I decided to type it after all…


    <?php
    /* Plugin Name: wpautop control
    * Plugin URI: http:/wpbits.wrodpress.com
    * Author: Leonid Mamchevkov
    * Author URI: http://mamchenkov.net/
    * Version 1.0
    * Description: Diables wpautop on the_content filter
    */

    remove_filter('the_content', 'wpautop');

  15. Hans said

    It seems it does not work on 2.3.1

  16. Hans,

    yes, probably. I wrote this post before 2.3.1 came out. I didn’t have the time yet to go through the changes of the newer WP. Probably I’ll do it over the Christmas holidays. :)

  17. Hans said

    Great. I will be looking forward to it.

  18. […] tried to search for solution for few times. I found this post A look at WordPress filters. Disable wpautop() on post formatting. by Leonid Mamchenkov. It was not a correct solution for this problem. That post only point out how […]

  19. mOLOk said

    Thanks from another Vim user :)
    I use ViewSourceWith plugin for firefox to use vim to edit ANY textbox, including the “Write Post” one of wordpress :)
    Finally I can write my posts with a decent editor.
    HTH

  20. […] per quelli come me che usano editor esterni, editor seri come Vim. Soluzione: Mi ha aiutato questo post in cui mi sono reso conto di non essere l’unico a soffrire questo comportamento. Dal sito di […]

  21. I LOVE you!

    I commented out the wordautop stuff everywhere except for comments, on the theory that people expect certain behavior from WP comments.

    I can know write actual HTML the way I want it without it throwing piles of random paragraph tags around my images and whatnot.

    YOU ROCK!!!!

  22. venz said

    My problem is this. I migrated data from a Drupal blog. I need WP to display the posts according to its html markup.

    However, I want the usual WP formatting rules applied for new posts. Is there a way to do this?

  23. fenyx said

    Thanks for this work.
    I wanted to find the same result as on the TinyMCE test page but I still can’t. I explain: when I paste my text to the Visual Editor then I switch to HTML Editor, I can see that all the line breaks and empty lines are replaced by paragraphs or . But when I do the same in the WordPress v2.6.3 Visual Editor, my blank lines stay just blank lines even in HTML view. And as you know, a blank line provides no result at all in HTML.

    Do you hav any idea of how I could make my editor working the same way as the rel TinyMCE editor? Is it related to formatting.php or post-template.php by any way? I’m just a noob, sorry for that. But I really need your help to make my use of WordPress easier (and above all faster). Manually inserting and everywhere in a long article through HTML view is so annoying and could be so long…

    Mptre seems to have found a solution HERE. What do you think about it? I’ve searched a lot more infos about what is /n and /r but it looks like chinese language to me. Is /n same as what I do by SHIFT+ENTER in Visual Editor?

    So much questions in this post :)

  24. fenyx said

    The missing HERE link, sorry

  25. fenyx said

    “are replaced by paragraphs or ” [br’ /]

  26. […] A look at WordPress filters. Disable wpautop() on post formatting. « WordPress Bits WordPress provides a number of ways to control how content is stored and displayed. There are themes, which I’m sure everybody has already heard of. There are widgets, which are picking up the fame. But there are things deeper in, which aren’t as famous. They are called filters. And WordPress has quite a few of them. (tags: wordpress howto tutorial filters tutorials useful programming article formatting) […]

  27. Atle said

    Yes, I know this is post is old, but anyway, I just wanted to say thanks for a great post! ;)

  28. […] 7月 2, 2009 in plugins http://wpbits.wordpress.com/2007/08/06/a-look-at-wordpress-filters-disable-wpautop-on-post-formattin… […]

  29. David said

    Was building my own front-end display to the blog part of my website, was looking to break out of wordpress functions and this is exactly what I was looking for, thanks a million.

  30. […] I got to know that this error was quite common. Since if you google it, there are a lot of links that are shown. Unfortunately, there isn’t a single one that point out the solution. The best reference I can found is this blog post, A look at WordPress filters. Disable wpautop() on post formatting. […]

  31. Laguna said

    Thank You, thank You!

    I use localization filter and I needed formated text with ” tag. So I needed two filters ‘localization’ and ‘the_content’. Now, thanks to You, I can use many wordpress filters:

    $content = $page_content->post_content;
    $content = apply_filters(‘the_content’, $content);
    $content = apply_filters(‘localization’, $content);
    echo $content;

    Thanks again

  32. thanks for the text for the plugin

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

Join 40 other followers

%d bloggers like this: