Postie and featured images

It took me a bit of messing about to get the blog how I wanted it with regards to looks and also ease of posting.

I need to be able to post from my iPhone, including media, mostly images.  I wanted to be able to post multiple images and also be able to set one of them as the “featured image” as that is a requirement of the theme I’m using.  The featured image is the one that shows on the post listing pages and the main page.

I decided to use the Postie plugin and blog via email.

Here is how I have it setup currently:

  • WordPress 3.2.1
  • Slightly modified Unstandard theme
  • Postie plugin – to blog via email (modfied – details below)
  • Javascript chat for wordpress plugin – like the old days!
  • Lightbox Gallery plugin – Postie adds images as a “gallery”, lightbox makes viewing more elegant
  • WPtouch plugin – to make viewing from mobile devices nicer – needs mods though!
  • WordPress Mobile Admin plugin – is useful for using the admin from mobile devices

Modifying Postie to set the “featured image”

Inside  postie/postie-functions.php  there is a function called  postie_media_handle_upload  – this function does what it says on the tin, it handles the media uploads!

When Postie is processing an email it will call that function once per image attachment so what we need to do is add some code to also populate the relevant part of the database to set the image as the “featured image”.

To do this we simply add this code to the end of the function (just before the “return” statement):

update_post_meta($post_id, '_thumbnail_id', $id);

This is a simple hack and does mean that each time an image is processed the code will set that one as the featured image for that post meaning the last attachment to be processed will become the featured one. I will quite often be including multiple images and couldn’t think of a simple way to specify which image is the one you want as the featured one so will have to work with this fact.  I have figured out on my iPhone how to get the picture I want to be the last one processed so can control this anyway.  I also haven’t tested what happens if you attach a video and that is the last one to be processed as the name of the function suggests it deals with video attachments too.

Next post:

Previous post:

Discuss - 7 Comments

  1. Slick. I’m having that idea.

  2. I honestly thought that was a pic of you at first when i saw it on my phone

  3. kevw says:

    Lol. That’s the guy that made the Postie plugin

  4. cla says:

    Hi, I had the same idea, and I’m trying to set postie. I think that it would be really useful, but I’m new in this word and I cannot find the exact line where I have to add the code. I put this here:

    $attachments["cids"][$cid] = array($file,
    count($attachments["html"]) – 1);
    update_post_meta($post_id, ‘_thumbnail_id’, $id);
    }
    }
    break;
    }
    }
    return($meta_return);

    But in this way, it doesn’t work!

    Thank you for this post!

  5. kevw says:

    Hi there,

    You need to add the line of code to the very end of the postie_media_handle_upload function.

    In my file it looks like this:

    // Save the data
    $id = wp_insert_attachment($attachment, $file, $post_id);
    if ( !is_wp_error($id) ) {
    wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
    }

    update_post_meta($post_id, '_thumbnail_id', $id);

    return $id;

    }

    I hope this helps but if not let me know!

  6. Anita says:

    Hi kevw,
    this is great! it is exactly what I have been loooking for.

    how do you upgrade the postie plugin without destroying the changes in the postie_media_handle_upload function?
    thanks, anita

  7. kevw says:

    Hi Anita,

    You have raised a good point that I haven’t mentioned in my post. I upgraded Postie one time when an update was released and it does indeed lose any changes that are made.

    I’m not aware of any way to keep custom changes to a plugin when upgrading, from what I can gather an upgrade simply replaces the files with new ones, effectively overwriting any changes.

    I guess the options are to either make the change again after upgrading or don’t upgrade!

Add a Comment

Your email address will not be published. Note marked required (*) fields.

*