WordPress

Writing your own custom filters for postie

In version 1.3.1 of postie, it is now easy to add custom filters to the content of your posts. Best of all, by using filters, you can easily write your own plugin, which will not be affected by upgrades to postie (as opposed to modifying the postie functions directly). There are two examples now included in version 1.3.1 of postie, in the filterPostie.php file. Here is how to write your own filter.

  1. Copy the filterPostie.php into your plugins directory
  2. Change the metadata at the top of the file, such as the author and plugin name
  3. Modify the example functions to your liking, or create your own
  4. Activate the plugin

A few more details. After modifying the headers, you might have something like the following:

/* Plugin Name: My own Postie Filter Plugin
URI: http://mywebsite
Description: Adds a custom field to each post called postie
Version: 0.1 Author: Joe Bloe
Author URI: http://mywebsite.com
*/

Just as with any filter for wordpress, you have 2 parts.

  1. A function which receives an argument, makes some modifications (filtering) and returns a result
  2. A function call to add this new function to the list of filters for a certain action

As you can see in the example file, I have created two functions, filter_title, and filter_content. After creating these functions, I then “hook” them into postie using the add_filter function.

add_filter('postie_post', 'filter_title');
add_filter('postie_post', 'filter_content');

Now lets create our new function to add a custom field

function add_custom_field($post) {
  //this function appends "(via postie)" to the title (subject)
  add_post_meta($post['ID'], 'postie', 'postie');
  return ($post);
}

Finally, we let wordpress know that this function should filter any post that is sent via postie

add_filter('postie_post', 'add_custom_field');

Currently our function is really not that useful, since the key and value of our custom field are the same. However, there is really very little limit to what we can do with filters. Perhaps a more useful function would be an auto- tagger. Suppose I often write about cooking, wordpress, and latex (which I do). But when I am posting via e-mail, I frequently forget to add tags. We could write a little filter function to read the body of the post, and automatically add tags depending on the content. Here is what it might look like:

function auto_tag($post) {  
  // this function automatically inserts tags for a post  
  $my_tags=array('cooking', 'latex', 'wordpress');  
  foreach ($my_tags as $my_tag) {    
    if (stripos($post['post_content'], $my_tag)!==false)      
      array_push($post['tags_input'], $my_tag);  
  }  
  return ($post);
}
add_filter('postie_post', 'auto_tag');

I’m sure that users will come up with many other interesting filters. If you develop any you would like to share, please post them on the postie forum