Skip Navigation

[Resuelto] What is the slug for a featured image?

This support ticket is created hace 1 año, 3 meses. 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.

Hoy no hay técnicos de soporte disponibles en el foro Juego de herramientas. Siéntase libre de enviar sus tiques y les daremos trámite tan pronto como estemos disponibles en línea. Gracias por su comprensión.

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)

Este tema contiene 4 respuestas, tiene 3 mensajes.

Última actualización por Waqar hace 1 año, 2 meses.

Asistido por: Waqar.

Autor
Mensajes
#2634409

I am following this post https://toolset.com/forums/topic/featured-image-size-validation-in-form/ but it asks me to replace with the slug of my featured image. I am using just the custom default WordPress featured image field. What do I need to put into the snippet of code used here: https://toolset.com/forums/topic/featured-image-size-validation-in-form/

#2634677

Nigel
Supporter

Idiomas: Inglés (English ) Español (Español )

Zona horaria: Europe/London (GMT+00:00)

Hi there

That's an old thread, and some of the information is outdated, in particular the details relating to ajax updates to pre-load images. That is now handled via the WordPress media uploader, if chosen in the form settings, and only the non-ajax method (the images are uploaded along with the form submission) is amenable to validation using the suggested code.

In that forum thread there are two code suggestions, and it is the first of the two that you would need to use (which uses the cred_form_validate hook). And looking at the code, it appears it already targets the featured image field (rather than a custom image field).

Did you try that code?

#2634725

I just tested it again and the form uploads images outside of the parameters set. enlace oculto

#2634935

Hi,

Thank you for sharing this update.

I can perform some tests on my website and suggest adjustments to that code. But, first I'll need to see how this form and its fields are set up in the admin area.

Please share the temporary admin login details in reply to this message.

Note: Your next reply will be private and making a complete backup copy is recommended before sharing the access details.

regards,
Waqar

#2635967

Thank you for sharing the access details.

I've performed some testing and research with a similar form on my website and here are the findings:

The filter 'cred_form_ajax_upload_validate' (that the code from the referenced ticket is using), specifically targets the event when a new image is uploaded into the WordPress media library through Toolset Forms.

For this filter to trigger, the following conditions need to be met:

1. In the form's settings, the 'Use the WordPress Media Library manager for image, video, audio, or file fields' option needs to be checked.

AND

2. The new image should be uploaded as it won't fire if an existing image from the media library is selected.

AND

3. The visitor who is submitting the form, needs to be logged in as a user. For guests (non-logged-in), this filter won't be triggered because the guests can't upload images into the media library and a standard image upload field is shown when they are submitting the form.

So, if you need to target the featured image field in a more generic way, you can switch to the 'cred_form_validate' hook, instead:
( ref: https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate )

Example:


add_filter('cred_form_validate','my_validation',10,2);
function my_validation($error_fields, $form_data)
{
	//field data are field values and errors
	list($fields,$errors)=$error_fields;
	//validate if specific form 
	if ( ($form_data['id']==356) ) {
		if(!empty($fields['_featured_image']['file_data'])) {
			list($width, $height, $type, $attr) = getimagesize($fields['_featured_image']['file_data']['tmp_name']);   
			if ($width != 500 && $height != 500) {
				//set error message for featured image
				$errors['_featured_image'] = 'Image size must be 500 * 500, your image dimensions are '.$width.' * '.$height;
			}
		}  
	}
	//return result
	return array($fields,$errors);
}

This should do the trick.

#2636005

Thank you!