Category: How To

Action: postie_session_start

This action gets called at the beginning of each Postie session. I.e. when Postie starts the process to check for emails and turn them into posts.

add_action('postie_session_start', 'my_postie_session_start_action');

function my_postie_session_start_action() {
    //get ready to process a bunch of emails
}

postie_log_* Actions

postie_log_error & postie_log_debug

These filters are called when Postie has some information about what is happening. postie_log_error is called if Postie encounters an error reported by WordPress. postie_log_debug is called throughout the Postie process when something of interest occurs.

add_action('postie_log_error', 'my_postie_log_error_action');
add_action('postie_log_debug', 'my_postie_log_debug_action');

function my_postie_log_error_action($message) {
    //log the error somewhere
}

function my_postie_log_debug_action($message) {
    //log the debug message somewhere
}

Action: postie_comment_after

This filter is called just after the comment is added to the post.

Parameters:

  • $comment – an array that represents the comment. See this for more information about the data in the $comment array.
  • $comment_ID – the id of the comment. Note that this parameter is only available in Postie version 1.9.55 or later.
add_filter('postie_comment_after', 'my_postie_comment_after', 10, 2);

function my_postie_comment_after($comment, $comment_ID) { 
    echo "postie_comment_after called<br>\n";
}

Filter: postie_comment_before

This filter is called just before the comment is added to the post. See this for more information about the data in the $comment array.

add_filter('postie_comment_before', 'my_postie_comment_before');

function my_postie_comment_before($comment) { 
    $post = get_post($comment['comment_post_ID']);
    $title = $post->post_title;
    echo "postie_comment_before called<br>\n";
    return $comment;
}

Filter: postie_gallery

This filter is called when Postie inserts a gallery shortcode into the post (which only ever happens if the “Automatically insert image gallery” setting is turned on). You can use this filter to generate an alternate gallery shortcode. At the point it is called there is only a placeholder post, e.g. the content and metadata is not set yet, however, all the media have been attached.

The $shortcode parameter is the existing shortcode (typically [gallery]).

The $postid parameter is the ID of the post.

add_filter('postie_gallery', 'my_gallery_filter', 10, 2);

function my_gallery_filter($shortcode, $postid) {
 $shortcode = "[mygallery format='flex']";
 return $shortcode;
}

Action: postie_file_added

This action is called after Postie successfully extracts an email attachment and adds it as a media item to the post. At the point it is called there is only a placeholder post, e.g. the content and metadata is not set yet.

add_action('postie_file_added', 'my_fileadded', 10, 3);

function my_fileadded($postid, $attachmentid, $file_array) {
    $fileinfo = wp_check_filetype(get_attached_file($attachmentid));
    $file = $fileinfo['file'];
    $url = $fileinfo['url'];
    $mimetype = $fileinfo['type'];
    //Do something like set featured image
    if (strpos($mimetype, 'image', 0) === 0) { //only if attachment is an image
        set_post_thumbnail($postid, $attachmentid);
    }
}

Adding your own Postie shortcodes

The Postie Shortcodes AddOn allows you to create your own shortcodes that Postie will use when turning your email into a post.

Not a programmer? I do custom development and can help you. Contact me

Step one is to register an action hook named “postie_register_shortcode” – something like this:

add_action('postie_register_shortcode', 'registermycustompostieshortcodes');
function registermycustompostieshortcodes(){
    add_shortcode('myshortcode', 'myshortcodefn');
}

function myshortcodefn() {
    return "shazam!";
}

You can put this code in the postiefilter.php file or your own plugin or theme. The shortcode will be evaluated after the email has been processed, but before it has been saved (during the postie_post_before filter). This is very different than normal shortcodes which are processed every time the post is displayed.

Note that you should not call add_shortcode() outside the postie_register_shortcode action otherwise Postie will not know about it and your post won’t look right.

Here is a more complex and realistic example:

add_action('postie_register_shortcode', 'registermycustompostieshortcodes2');
function registermycustompostieshortcodes2(){
    add_shortcode('forceadmin', 'myforceadmin');
}

function myforceadmin() {
    global $postie_post;
    $postie_post['post_author'] = 0; //admin user
    return '';
}

Note the global $postie_post declaration. This global variable gives you access to all the fields about to be used to save the post. Of note is the fact that the post does already have a post id assigned in case you need to take some action that requires a post id. You can access the post id like so: $postie_post[‘ID’]. See this page of a list of possible array values.