A-  A  A+ RSS Feed

Deep Thoughts by Robert Felty

thoughts on wordpress, latex, cooking et alia
August 24th, 2009

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

6 Responses to “Writing your own custom filters for postie”

  1. On August 25th, 2009 at 5:15 pm
    Fat Cow wrote:

    Hello!

    I have translate your plugin into russian language and send You files. but You ignore me. when You publish this? Please answer me to email!

  2. On August 25th, 2009 at 6:13 pm
    robfelty wrote:

    Fat Cow,

    Thanks for the translation. I hope to include it in the next release of postie.

    Rob

  3. On September 15th, 2009 at 10:25 am
    elias wrote:

    great work!!!!!!! i love this plugin, it’s exactly what i was looking for!
    there is only one problem with the latest release 1.3.3, maybe you can have a look at the forum for wordpress-plugins:
    http://wordpress.org/support/topic/281891?replies=8#post-1212440
    seems like there have more people the same problem and no solution :-(
    for the while i step back to version 1.3.2 until a sloution for the probelm with the attachments is available.

    greetings elias.

  4. On September 24th, 2009 at 11:03 am
    Max wrote:

    Hello, how do you get youtube embed videos to work? Thanks in advance =D

  5. On October 10th, 2009 at 3:45 pm
    Dave Bergschneider wrote:

    Can Postie set the post status to “Pending” instead of Published by default? I can’t seem to figure out how to do that.

  6. On February 23rd, 2010 at 8:24 am
    paolo wrote:

    Dear
    you are a genius, self seats and is exceptional in all its aspects, easy to use and track you have been really good … congratulation and thanks.
    I have a question: You can delete a post via email?
    thanks Paul

Leave a Reply

You can use some basic tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>
To get pretty code, use <code lang='php' > (or 'css' or 'perl' or whatever)