Custom Filters in Habari

I have been working on a custom theme for Habari, and I recently came across a really interesting feature. There are references to statements like $post->content_out and $post->content_excerpt scattered throughout other themes, and these look like straightforward property references.

However, Habari is sneaky behind the scenes. In this example, the post class is dynamically looking at the property name and splitting apart the name into two pieces: the actual property name, 'content', and then the filter 'out'. Then it checks to see if there is a filter sink in a plugin or theme defined as `filter_post_content_out`. It then calls that method and uses the return value for the value of `$post->content_out`.

This is really slick, and lets you define your own property values that you can reference in your templates. Here's an example method I added to my theme.php to create a link from the post's permalink property:

    public function filter_post_permalink_html($permalink, $post)    {   
        return '' . $post->title_out . '';

which is referenced in my template code as:

  • permalink_html?>
  • You can also modify the normal property value with a filter. If I wanted to make every permalink reference a full URL (which wouldn't be a good idea), I could name my method filter_post_permalink, and it would be applied every time someone used $post->permalink.

    The advantage to using this method as opposed to creating your own method in theme.php and calling it from the template is twofold. Using this filter method, other plugins and themes can also apply filters to the same property value and the result will be built up and returned. Also, other plugins and themes can just use the standard properties, and any of your changes will be included (as in the above $post->permalink example).