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

This entry was posted in wordpress and tagged . Bookmark the permalink.

12 Responses to Writing your own custom filters for postie

  1. Fat Cow says:

    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. robfelty says:

    Fat Cow,

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

    Rob

  3. elias says:

    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. Max says:

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

  5. 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. paolo says:

    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

  7. Ann says:

    Rob:

    Thanks for the great plugin!

    I have been using postiefilter.php to extract the poster’s name, but can only retrieve the sender’s email address. I’d rather not publish that on my blog, but it would be really nice to extract the sender’s name from the email.

    Can you add that as one of the filter variables?

    Thanks,

    Ann

  8. Ann says:

    Rob:

    I solved the question above by adding this to postiefilter.php:

    function filter_emailauthor($post) {
    //this function appends the sender’s name to the posted message, if a name is supplied
    //it appends nothing if there is no name, for MMS this avoids publishing sender’s phone number
    if (!empty($post[‘comment_author’])) {
    $link = “\n” . “photographed by ” . $post[‘comment_author’];
    $post[‘post_content’]= $post[‘post_content’] . $link;
    }
    return ($post);
    }

  9. Ann says:

    Rob:

    This is for postie version 1.4.1

    I’m posting images via email and MMS using Postie. On a recent installation, Postie is not substituting values for {width} {height}. This doesn’t break on all browsers, but for some browsers the image doesn’t get displayed. I think it breaks on MSIE for PC – a lot of users, and the problem has been consistently repeated on over 200 images. I can supply the

    This is the code I’ve placed the Postie image template box:

    [caption id="attachment_{ID}" align="aligncenter" width="{WIDTH}" caption="{CAPTION}"][/caption]

    All other variables are substituted correctly. In addition to correct substitution of the width and height of the image, I need image dimensions scaled according to my blog’s media settings so that images fit my theme dimensions. For example all iPhone images forwarded through MMS from iPhone Photos have the dimensions 1024 x 768. My blog is set up to scale Medium sized images to 694 x 400.

    Can you help with this?

    Thanks!

  10. Ann says:

    The code snippet above was modified because of the image tag – sorry didn’t notice.

    Here’s the full text of my custom image template in Postie with ** for the open/close html image tag:

    [caption id="attachment_{ID}" align="aligncenter" width="{WIDTH}" caption="{CAPTION}"] ** img src=”{MEDIUM}” alt=”{TITLE}” title=”{TITLE}” width=”{WIDTH}” height=”{HEIGHT}” class=”aligncenter size-medium wp-image” ** [/caption]

  11. Ann says:

    This is a resolution for my post above.

    In the documentation at this link:
    http://wordpress.org/extend/plugins/postie/other_notes/
    you mention {WIDTH} and {HEIGHT}

    postie-functions.php actually uses var names
    {MEDIUMWIDTH} and {MEDIUMHEIGHT}

    When I use those in my custom image template, I get the correct number, but the text ‘px’ is appended, which would also break the resulting code in some browsers.

    So to avoid this I’ve modified postie-functions to delete the appended ‘px’ from the values set for {MEDIUMWIDTH} and {MEDIUMHEIGHT}

  12. airclimber says:

    It appears to me that this website doesn?t download on a Motorola Droid. Are other people getting the same problem? I like this web site and don?t want to have to miss it whenever I?m away from my computer.