Skip Navigation

[Résolu] Adding default featured image per CRED form/CPT

This support ticket is created Il y a 1 année et 3 mois. There's a good chance that you are reading advice that it now obsolete.

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.

Aucun de nos assistants n'est disponible aujourd'hui sur le forum Jeu d'outils. Veuillez créer un ticket, et nous nous le traiterons dès notre prochaine connexion. Merci de votre compréhension.

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)

Ce sujet contient 1 réponse, a 2 voix.

Dernière mise à jour par Waqar Il y a 1 année et 3 mois.

Assisté par: Waqar.

Auteur
Publications
#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: lien caché )

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!