I found this https://toolset.com/forums/topic/default-image-if-user-do-not-add-one-in-cred-form/ but I need to be able to do this per custom post type. Each CPT has its own CRED Post form I created with Toolset. Can you assist with letting me know how to add additional CPTs each with it's own default featured image that I can set per CPT when a user completes a CRED form from the front-end and doesn't upload a featured image?
Hi,
Thank you for contacting us and I'd be happy to assist.
The other support thread you referenced was about setting a default image for an image custom field. For the featured image, the code will need to be changed to use the 'set_post_thumbnail' function:
https://developer.wordpress.org/reference/functions/set_post_thumbnail/
For example, suppose you have two CPTs 'Books' and 'Shops'. The form ID for the form to add a 'Book' is '123' and for the 'Shop' it is '789'. And likewise, the default image's attachment ID for the 'Book' CPT is '456' and for the 'Shop' CPT, it is '101112'.
( ref: hidden link )
The custom function in this case will look like this:
add_action('cred_save_data', 'func_set_default_image_cred_form',10,2);
function func_set_default_image_cred_form($post_id, $form_data) {
// if a specific form for 'Books'
if ($form_data['id']==123) {
if( empty($_POST['_featured_image']) ) {
// default featured image attachment ID for 'Books'
$attachment_id = 456;
set_post_thumbnail( $post_id, $attachment_id );
}
}
// if a specific form for 'Shops'
if ($form_data['id']==789) {
if( empty($_POST['_featured_image']) ) {
// default featured image attachment ID for 'Shops'
$attachment_id = 101112;
set_post_thumbnail( $post_id, $attachment_id );
}
}
}
Using the same example, you can keep adding the additional 'if' blocks for more CPTs.
I hope this helps and please let me know if you need further assistance.
regards,
Waqar