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.

Action: postie_post_after

This action is called just after Postie saves the post.

At the point the action is called the post has been created and all attachments have been uploaded and associated with this post.

There is one parameter to this filter: $post

The $post parameter is an array describing the post extracted from the email.

Essentially any field described here can show up.

Any changes you may make to the array elements are ignored. If you want to change the post use postie_post_before.

<?php 
function my_postie_post_after($post) {
    //do something here like update the meta data
    add_post_meta($post['ID'], 'mymeta', 0);
}

add_action('postie_post_after', 'my_postie_post_after');
?>

Array index Notes
post_author the WordPress user that is the author of the post.
post_date the localized date of the post.
post_date_gmt the GMT/UMT date of the post.
post_content the actual content of the post.
post_title the post title (email subject line).
post_name the sanitized post title.
post_excerpt the post excerpt.
ID the post id.
post_status the post status.
post_category an array of category IDs
ping_status
comment_status
post_type

Note that if Postie detects a email reply it will add a comment to the original post and the data in the array will be as follows:

comment_author
comment_post_ID
comment_author_email
comment_date
comment_date_gmt
comment_content
comment_author_url
comment_author_IP
comment_approved
comment_agent
comment_type
comment_parent
user_id

See this for more details.

Filter: postie_author

This filter is called after Postie has determined who the post author should be.
At the point the filter is called there is no post created.
The parameter is a WordPress user ID.
add_filter('postie_author', 'my_filterAuthor');

function my_filterAuthor($userId) {
    //do some user validation logic
    return $userId
}

See also:

Filter: postie_filter_email2

This filter is called when Postie extracts the “from” email address. Typically you would use this filter if you wanted to decide between the “from”, “to” and “reply-to” email addresses.
At the point the filter is called there is no post created. If the “to” or “reply-to” headers do not exist in the source email the values will be a 0 length string (”).
The parameters are each single email address.
add_filter('postie_filter_email2', 'my_filterEmail2', 10, 3);

function my_filterEmail2($fromEmail, $toEmail, $replytoEmail) {
    //do some email validation logic
    return $fromEmail;
}

Remember that $toEmail and $replytoEmail may be blank (empty string).

See also: