On a post form I use an image upload field and allow multiple instances of this field. How can I use the first uploaded image as the (standard WordPress) featured image?
This would require the use of custom coding when the form is being submitted to pick out the first image from the array of images being sent.
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));
}
}
}
Add the above to your toolset custom code page in Toolset -> Settings -> Custom Code. 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.
Hi Shane,
Thank you for your quick reaction. However the featured image does not appear.
This is probably due to the fact that I use an image field where I allow multiple instances. I would not know how to use a repeatable image field.
Could you enlighten me?
Thamks,
Jos
This is the code I added: hidden link
To see the form you'll have to login (the form allows members to post without reviews). So please send me a reply which will be invisible.
I believe I spot the issue. In the code you have the form id being 227 when the actual ID of the form is 276.
Could you update this in the code and let me know if it helps.
Secondly i'm not sure which page you've added the form itself on so if changing the ID doesn't please let me know the page the form is on so that I can test this out.
The form is, as you said, id:276 is on page "maak een nieuw bericht" id: 270. It fills the CPT biljartberichten. You'll find the archive for biljartberichten in the menu under 'nieuws'.
There is something strange with the attached images (slug= 'afbeeldingen'). In the backend they show with the respective posts (biljartberichten). On the frontend they only show with the first post 'testfoto'.