How to set the first uploaded image of a front end form which is a repeating field without using the WordPress Media library mode to be the posts featured image?
Solution:
Add the custom code below to Toolset:
add_action( 'cred_save_data', 'tssupp_make_featured_image', 10, 2 );
function tssupp_make_featured_image( $post_id, $form_data ){
$image_field = 'pictures'; // edit field slug
if ( in_array( $form_data['id'], array( 123 ) ) ) { // edit form ID
// get the images
$images = get_post_meta( $post_id, 'wpcf-'.$image_field, false );
// get the attachment ID of the first image
if ( is_array($images) ){
$first = attachment_url_to_postid( $images[0] );
if ( $first > 0 ){
// set the featured image
update_post_meta( $post_id, '_thumbnail_id', $first );
// delete the first custom field image
delete_post_meta( $post_id, 'wpcf-'.$image_field, $images[0] );
}
}
}
}
Replace "id" with the form ID and replace "pictures" with the repeating custom field that you use for the images.
There are two lines of buttons for some users in the Editor and one line for others.
Solution:
Please click the icon in the editor annotated in the attached screenshot and you will see the second line.
The editor has one line by default and when the user clicks that toggle button it shows an additional line and it will be saved for the user in question from that point on.