Support Forum
Support › MB Frontend Submission › Use Image Upload field to set the Featured Image of the post
I'm using the frontend submission form. I would like to use the IMAGE UPLOAD field (limit it to 1 image) to set the featured image of the CPT post that's being created.
I don't know how. The image gets submitted and saved into the post, but how can I set is as featured image?
Hi Concan,
You can use the field ID _thumbnail_id
to set the image uploaded as a featured image. Get more details on the documentation https://docs.metabox.io/extensions/mb-frontend-submission/#reorder-post-fields
Hi Long - can you break this down a bit for me?
I have a custom field set for a CPT. This set includes an Image Upload with these settings:
- ID: _thumbnail_id
- Max number of files: 1
I have a snippet with the following where "candidate" is the Field Group ID:
add_filter( 'rwmb_frontend_validate', function( $validate, $config ) {
if ( 'candidate' !== $config['_thumbnail_id'] ) {
return $validate;
}
if ( empty( $_POST['image_upload_field'] ) ) {
$validate = 'Please select at least one image to upload'; // Return a custom error message
}
return $validate;
}, 10, 2 );
Can you tell me where I went wrong? The image is not saving to the post at all (as a regular field or as a Featured Image). This is true when using the front-end form or when editing the post from the backend.
Thanks in advance for your help!
Hi Kara,
I think there are two issues with your code.
The code $config['_thumbnail_id']
should be $config['id']
to check the field group ID.
The code $_POST['image_upload_field']
should be $_POST['_thumbnail_id']
to check the field ID.
Thanks Long -
I made the changes but the image is still not saving to the post, or as a featured image.
It is, however, saving to the Media Library.
add_filter( 'rwmb_frontend_validate', function( $validate, $config ) {
if ( 'candidate' !== $config['id'] ) {
return $validate;
}
if ( empty( $_POST['_thumbnail_id'] ) ) {
$validate = 'Please select at least one image to upload'; // Return a custom error message
}
return $validate;
}, 10, 2 );
In case it's useful, here are screenshots of the custom field:
https://www.awesomescreenshot.com/image/28499524?key=8ff840b5f2d8f12f5597455d36426661
https://www.awesomescreenshot.com/image/28499595?key=21ed4f1eb276c229c449d473d04f1a16
Hi Kara,
Can you please share the code that creates the field group candidate
on your site? And please make sure that the featured image is enabled for that CPT.
The code above helps you to validate the field image upload to not empty, it does not help to set the featured image after submitting the form.
Hi Long - see below. I did notice that I had "candidate" instead of "candidates" in my snippet. I updated that but it didn't fix the issue.
The CPT is "Candidate" and featured image is enabled.
The Custom field group id is "Candidate"
For the field group:
<?php
add_filter( 'rwmb_meta_boxes', 'your_prefix_function_name' );
function your_prefix_function_name( $meta_boxes ) {
$prefix = '';
$meta_boxes[] = [
'title' => __( 'Candidates', 'your-text-domain' ),
'id' => 'candidates',
'post_types' => ['candidate'],
'fields' => [
[
'name' => __( 'First Name', 'your-text-domain' ),
'id' => $prefix . 'first_name',
'type' => 'text',
],
[
'name' => __( 'Last Name', 'your-text-domain' ),
'id' => $prefix . 'last_name',
'type' => 'text',
],
[
'name' => __( 'Headshot', 'your-text-domain' ),
'id' => $prefix . ' _thumbnail_id',
'type' => 'image_upload',
'image_size' => 'medium_large',
'max_file_uploads' => 1,
'force_delete' => true,
'max_status' => false,
],
[
'type' => 'divider',
],
[
'name' => __( 'Candidate Statement', 'your-text-domain' ),
'id' => $prefix . 'candidate_statement',
'type' => 'wysiwyg',
'label_description' => __( 'Please tell us why you wish to service in this office. You may include actions you wish to promote, or anything you would like members to know. Limit 500 words.', 'your-text-domain' ),
'required' => true,
],
[
'type' => 'divider',
],
[
'name' => __( 'Relevant Experience', 'your-text-domain' ),
'id' => $prefix . 'relevant_experience',
'type' => 'wysiwyg',
'label_description' => __( 'Please describe your relevant experience - what you bring to the office. Limit 500 words.', 'your-text-domain' ),
'required' => true,
],
[
'type' => 'divider',
],
[
'name' => __( 'Social Media', 'your-text-domain' ),
'id' => $prefix . 'social_media',
'type' => 'group',
'fields' => [
[
'name' => __( 'LinkedIn Profile', 'your-text-domain' ),
'id' => $prefix . 'linkedin_profile',
'type' => 'url',
],
[
'name' => __( 'Twitter Handle', 'your-text-domain' ),
'id' => $prefix . 'twitter_handle',
'type' => 'url',
],
[
'name' => __( 'Facebook Profile', 'your-text-domain' ),
'id' => $prefix . 'facebook_profile',
'type' => 'url',
],
[
'name' => __( 'Reddit Handle', 'your-text-domain' ),
'id' => $prefix . 'reddit_handle',
'type' => 'url',
],
],
],
[
'type' => 'divider',
],
[
'name' => __( 'Organizational Affiliations', 'your-text-domain' ),
'id' => $prefix . 'organizational_affiliations',
'type' => 'group',
'clone' => true,
'add_button' => __( 'Add another organization', 'your-text-domain' ),
'fields' => [
[
'name' => __( 'Name of Organization', 'your-text-domain' ),
'id' => $prefix . 'name_of_organization',
'type' => 'text',
],
[
'name' => __( 'Your Role', 'your-text-domain' ),
'id' => $prefix . 'your_role',
'type' => 'text',
],
],
],
[
'type' => 'divider',
],
[
'name' => __( 'Skills and experience you can bring', 'your-text-domain' ),
'id' => $prefix . 'skills_and_experience_you_can_bring',
'type' => 'checkbox_list',
'options' => [
'Board development' => __( 'Board development', 'your-text-domain' ),
'Program development' => __( 'Program development', 'your-text-domain' ),
'Financial management' => __( 'Financial management', 'your-text-domain' ),
'Staffing / HR' => __( 'Staffing / HR', 'your-text-domain' ),
'Fundraising' => __( 'Fundraising', 'your-text-domain' ),
'Strategic planning' => __( 'Strategic planning', 'your-text-domain' ),
'Lobbying/Legislation' => __( 'Lobbying/Legislation', 'your-text-domain' ),
'Training' => __( 'Training', 'your-text-domain' ),
'Marketing' => __( 'Marketing', 'your-text-domain' ),
'Volunteer management' => __( 'Volunteer management', 'your-text-domain' ),
'Other Skills (not listed above)' => __( 'Other Skills (not listed above)', 'your-text-domain' ),
],
'required' => true,
],
[
'name' => __( 'Additional skill not listed above', 'your-text-domain' ),
'id' => $prefix . 'additional_skill_not_listed_above',
'type' => 'text',
'label_description' => __( 'One per line', 'your-text-domain' ),
'clone' => true,
'add_button' => __( 'Add another skill', 'your-text-domain' ),
],
[
'type' => 'divider',
],
[
'name' => __( 'Declaration of Accuracy', 'your-text-domain' ),
'id' => $prefix . 'declaration_of_accuracy',
'type' => 'checkbox_list',
'options' => [
'I hereby declare that the information provided is true and correct. I also understand that any willful dishonesty may lead to removal from the election process and the NSITSP at large.' => __( 'I hereby declare that the information provided is true and correct. I also understand that any willful dishonesty may lead to removal from the election process and the NSITSP at large.', 'your-text-domain' ),
],
'required' => true,
],
],
];
return $meta_boxes;
}
My current validation snippet:
add_filter( 'rwmb_frontend_validate', function( $validate, $config ) {
if ( 'candidates' !== $config['id'] ) {
return $validate;
}
if ( empty( $_POST['_thumbnail_id'] ) ) {
$validate = 'Please select at least one image to upload'; // Return a custom error message
}
return $validate;
}, 10, 2 );
Hi,
There is a typo mistake when registering the custom field, the ID shouldn't have the space
current: 'id' => $prefix . ' _thumbnail_id',
correct: 'id' => $prefix . '_thumbnail_id',
Please read more on the documentation https://docs.metabox.io/field-settings/