Use first uploaded image as featured image

Author: 

Shane


Description: 

When using a cred form to collect data and images, and you want to collect a number of images  you can assign the first uploaded image as featured image.

Add the code in PHP to your toolset custom code page in Toolset -> Settings -> Custom Code (or in your functions.php). Once you’ve added the code you will need to replace the 13 with ID of your form and the “wpcf-repeatable-images” with the slug of your repeatable image field keeping the wpcf- prefix.

Once you have done this you will need to activate the snippet. (not necessary in functions.php).




Kind:

PHP, Toolset


PHP:

add_action('cred_save_data', 'set_image_as_featured',10,2);
function set_image_as_featured($post_id, $form_data)
{
// if a specific form
if ($form_data['id']==13)
{
if (isset($_POST['wpcf-repeatable-images']))
{
$image = $_POST['wpcf-repeatable-images'][0];
// Save the first image as featured image
set_post_thumbnail($post_id,attachment_url_to_postid($image));

}
}
}


URL: