Filters in WordPress

Having just read the very interesting and throughout explaination by Michel Fortin of his problems with the WordPress filters when trying to get [PHP Markdown][] right, I think it’s amazing that it works at all!

His experience together with the recent discussion started by Denis de Bernardy about the plugin system (and specifically the hooks offered to plugins) seems to suggest that the whole system could use some rethinking. (And when doing so, then please document it! :-)


  1. Denis de Bernardy:

    I wish… ;) I might fork the plugin system with my CMS theme’s next version.

  2. Martin Geisler:

    I’ve been doing some thinking myself about how a plugin system should
    work. For the filters it’s evident that it is too simple to just have
    a chain of functions doing “stuff” to the text. We need more
    structure, so that the “text” is in a well-defined format, and we need
    to define what you can do to this text.

    Perhaps this could be done by defining an [XML][] format for the text,
    and then all filters would modify this? One could require that the
    text is [XHTML][] at the beginning of the filter chain, and then the
    filters should have access to a proper parser and only modify the text
    through that. I’m not that much into this stuff, but I believe that
    this is what the XSL Transformation language lets you do.

    Of course, having all plugins modify the text using those style sheets
    would make it a lot harder to write plugins. Perhaps using full
    stylesheets should be optional — with the knowledge that the text is
    (valid!) XHTML, the plugins will still be able to parse and change it
    reliably without necessarily having to use an XML parser.

    I guess my ideas goes somewhat along the lines of the changes
    outlined by Michel Fortin on the mailinglist: draw a line
    somewhere and say: “Here we deal with the raw input from the user,
    and here we deal with valid XHTML.” If such a division was actually
    made, then lots of things would be easier.

  3. Denis de Bernardy:

    What you are describing is, more or less, what I’m doing with the latest versions of my plugins fuzzy recent plugins, as well as what I’m doing in the upcoming version of my theme.

    Basically, I do something along the lines of:

    • apply all filters
    • do php2xml
    • if display is xml, return xml
    • do xml2html
    • echo html

    I might bypass the filtering system entirely in a next version of my theme, and do something along the lines of the following instead:

    • $post = $post->apply_filters();
    • … (as previous)

    as suggested in the hackers list a couple of days ago. this would assume a limited number of use cases could be defined. I can name three:

    • ‘main’ e.g. for use as a tile on a single page
    • ‘abstract’ e.g. for use in a list as a search result
    • ‘title’, e.g. for use in a list where only a title and subtitle are needed

    there are possibly a few more. but not that many. in any event, this would significantly reduce the amount of plugin hooks, and do so for the better.

  4. Martin Geisler:

    Yep, I’ve noticed that your plugins are moving to XML, which I think it great! And as you say: make fewer plugin hooks but make them better, or to say the same using other words: “less is more” :-)

Leave a comment