SCENARIO : I allow to create posts from front-end. The form also has four image upload fields. I use the code pasted below for image attachments and setting the post thumbnail.

//insert attachments
    if ($_FILES) {
    foreach ($_FILES as $file => $array) {
        $newupload = insert_attachment($file,$pid);

 //attachment helper function   
 function insert_attachment($file_handler,$post_id,$setthumb='false') {
    if ($_FILES[$file_handler]['error'] !== UPLOAD_ERR_OK){ return __return_false(); 
        require_once(ABSPATH . "wp-admin" . '/includes/image.php');
        require_once(ABSPATH . "wp-admin" . '/includes/file.php');
        require_once(ABSPATH . "wp-admin" . '/includes/media.php');

        $attach_id = media_handle_upload( $file_handler, $post_id );
        //set post thumbnail
        if ($setthumb) update_post_meta($post_id,'_thumbnail_id',$attach_id);
        return $attach_id;

The post-thumbnail/featured image that is set through this code is the LAST image that is uploaded. I tried a google search for "set post thumbnail from frontend wordpress" and grazed through a lot of articles but none came close to what I am asking here. Majority of threads I came across at SE in regards to front-end posting, either talks about setting the featured image or about multiple uploads. I have also checked all the suggested questions which were prompted, while I was writing this question just to make sure if it has been asked before.'

If it matters, here is the html that is used in the form, pretty standard.

<input type="file" name="image-one" id="image-one"/>
<input type="file" name="image-two" id="image-two"/>
<input type="file" name="image-three" id="image-three"/>

REQUEST : It would be great if I could get a solution that helps to assign any chosen image-input-field as a featured-image but at this moment, at least what I need to set the FIRST image input to be set as a featured-image/post-thumbnail. Kindly suggest a solution.

Bottomline here is that I do not have an issue with setting post-thumbnail but question is about having a choice to choose any of the uploaded images as post-thumbnail or at least the first image, instead of the last image as set by the current code.

Footnote : I have just put here what came to my mind as necessary. If you want some more information, please let me know. Thanks in advance.

1 Answer 1


Re: set the FIRST image input to be set as a featured-image/post-thumbnail

Given that you know input name="image-one" is going to be your featured image, you can check for image-one as your $file_handler value in function insert_attachment after the call to media_handle_upload, then set the thumbnail. See the following code.

Do note that I set image-one as the field name to look for and pass it every time.

$thumbnail_field = 'image-one';
if ( ! empty( $_FILES ) ) {
    foreach ( $_FILES as $file => $array )
        $newupload = insert_attachment( $file, $pid, $thumbnail_field );

//attachment helper function   
function insert_attachment( $file_handler, $post_id, $set_thumb = false ) {
    if ( UPLOAD_ERR_OK !== $_FILES[ $file_handler ]['error'] )
        return false; 

    require_once ABSPATH . 'wp-admin/includes/image.php';
    require_once ABSPATH . 'wp-admin/includes/file.php';
    require_once ABSPATH . 'wp-admin/includes/media.php';

    $attach_id = media_handle_upload( $file_handler, $post_id );

    //set post thumbnail (featured)
    if ( $attach_id && $set_thumb )
        update_post_meta( $post_id, '_thumbnail_id', $attach_id );

    return $attach_id;

Re: assign any chosen image-input-field as a featured-image

I think this part isn't needed as you should just label the first image field as "Featured Image". It's possible the submitter doesn't want to set a featured image now.

In looking at the code above, replace line $thumbnail_field = 'image-one'; with the following.

if ( ! empty( $_POST[ 'thumbnail-field' ] ) )
    $thumbnail_field = esc_html( $_POST[ 'thumbnail-field' ] );
    $thumbnail_field = 'image-one';

This references a select field like below. Radio's and jQuery could be used as well to format and populate.

<label for="thumbnail-field">
    Thumbnail field
    <select name="thumbnail-field">
        <option value="image-one">1st</option>
        <option value="image-two">2nd</option>
        <option value="image-three">3rd</option>

Lastly, you might also refresh your memory on PHP's POST method uploads and check out WordPress's coding standards for writing more readable code.

  • For those of us a little new to programming and wordpress, what is it that you're saying in the final if statement? "if $attach_id has a number and $setthumb is true == ?" ?. The value of $file_handler, if dumped, is simply Array(), correct? In other words, "if none of these is empty, move forward?" Thanks!
    – user658182
    Jan 26, 2014 at 15:06
  • Opps… that == $file_handler shouldn't be there. The last if condition is basically, if there's an attachment $attach_id and I'm supposed to set a thumbnail, $set_thumb, then do so. Feb 7, 2014 at 0:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.