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.
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 forimage-one
as your$file_handler
value infunction insert_attachment
after the call tomedia_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.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.This references a
select
field like below. Radio’s and jQuery could be used as well to format and populate.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.