Skip Navigation

[Resolved] Adding default featured image per CRED form/CPT

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 1 reply, has 2 voices.

Last updated by Waqar 1 year, 2 months ago.

Assisted by: Waqar.

Author
Posts
#2633711

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?

#2633905

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

#2634511

Thanks! This worked!