[Resolved] Limit upload file size for repeating image custom field

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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

This topic contains 13 replies, has 3 voices.

Last updated by Rita 5 years ago.

Assigned support staff: Luo Yang.

Author
Posts
#280872

Hi there
I have created a CRED form that includes an image repeater so that my users can upload a set of images.

This is the code for the repeating custom image field in the CRED form:
<div class="cred-field cred-field-image-gallery-1">
<label class="cred-label">Your Photo/s<span style="color: #bf8104;">*</span></label>
[cred_field field="image-gallery-1" post="photo" value="Add another photo" urlparam="" class="js-wpt- repadd"]
</div>

This is how the CRED form looks:
hidden link

This is the code in the post's content template to display the array of images:
[types field="image-gallery-1" width="390" height="260" align="none" resize="crop" separator=" "][/types]

This is how the post looks:
hidden link

Its not ideal but it will do. (I am trying to avoid adding another plugin)

Is there a way to:

1. Limit/restrict file size?
2. Allow the user to select and upload multiple images rather than one image at a time?
3. Automatically generate a subfolder within the wp media folder that stores all the images each user uploads for any post type. so it would be something like media/username.
4. Where there is an array (from repeating image field), automatically group these sets within the user's sub-folder

#281255

Adriano
Supporter

Languages: English (English )

Timezone: America/Sao_Paulo (GMT-03:00)

Hi Rita,

1. Yes, sure. CRED has a powerful API and you should use that in order to limit file size. Please have a look: https://toolset.com/documentation/user-guides/cred-api/#cfv

There is a section "FILE / IMAGE FIELDS" for this kind of situation

2. No, that's not possible.
3. No, unfortunately you can't modify the way that's stored. That's from WordPress side.

Please let me know if you are satisfied with my reply and any other questions you may have.

Regards,

Adriano Ferreira

#281432

Hi Adriano

Sorry to be so 'beginner-esque' but can you help me to apply this? I always feel like I understand the logic - no problem! - but its never clear to me how to apply it...

So if I have a REPEATING custom field called 'image-gallery-1' and a CRED form 'id=3887' and I want to limit upload file size to maximum 100KB what would I add this to functions.php?

This?

add_filter('cred_form_validate','my_validation',10,2);
function my_validation($field_data, $form_data)
{

$fields['image-gallery-1']['value']=array(
'value'=>the url of the file (not final one if upload is currently done, or the actual file url if upload was done previously),

'file_data'=>array('my_file'=>

'size'=> 100kb,

), // only set if actual upload is performed, else empty
'file_upload'=>empty array/not used
);

);

#281917

Luo Yang
Supporter

Languages: English (English ) Chinese (Simplified) (简体中文 )

Timezone: Asia/Hong_Kong (GMT+08:00)

I tried the codes you mentioned above, like this:

add_filter('cred_form_validate','my_validation',10,2);
function my_validation($field_data, $form_data)
{
	list($fields,$errors)=$field_data;
	if ($form_data['id']==3887){
		//print_r($fields);
		print_r($fields['wpcf-image-gallery-1']);
		die();
	}
	return array($fields,$errors);
}

But can not get the image size, seems there is a problem to check the upload file size, I forward your question to our developers, to confirm if it is possible within current version of CRED, will feedback if there is any news.

#287145

Hi Luoy
Did you manage to get any insight back from the developers?
Regards
Rita

#287232

Luo Yang
Supporter

Languages: English (English ) Chinese (Simplified) (简体中文 )

Timezone: Asia/Hong_Kong (GMT+08:00)

It is fixed within latest version CRED 1.3.5, you can download it here:
https://toolset.com/account/downloads/

and try below codes:

function my_validation($field_data, $form_data)
{
    list($fields,$errors)=$field_data;
    if ($form_data['id']==123){
        if(isset($fields['wpcf-image-gallery-1']['file_data']['size'])){
			foreach($fields['wpcf-image-gallery-1']['file_data']['size'] as $k => $v){
				if($v>1000){
					$errors['image-gallery-1']='the image size should not be more than 1000 bytes';
				}
			}
		}
    }
    return array($fields,$errors);
}

Please replace 123 with your CRED form ID

#288097

Hi Luoy
I dropped your code into functions.php and applied it to a specific cred form. My CRED plugin is up to date so I did not download and install it again.
Then using the cred form I uploaded a file size 1,344,133 bytes (1.3 MB on disk) no problem at all.
Have I done something wrong? or missed something?
Rita

#288337

Luo Yang
Supporter

Languages: English (English ) Chinese (Simplified) (简体中文 )

Timezone: Asia/Hong_Kong (GMT+08:00)

I tested above codes in my localhost, it works fine, if you need more assistance for it, please duplicate same problem in a test site, and fill below private detail box with login details, I need a live website to test and debug. thanks

#288566

Hi Luoy

Thanks for your message. Ok so I checked everything again and found 2 things missed:

1. The first line of code: add_filter('cred_form_validate','telegram_image_size_validation',10,2);
2. I had changed the name of the custom field and forgotten that I had. So updated it.

NOW IT WORKS! THANK YOU. BRILLIANT. 🙂

I tried to apply the same code to another post type but it doesn't work. The only thing I can see that is different between the two post types is that the above custom image field is a repeating field and all the other post types are not repeating fields.

Here is an example of my 'story' post type with a non-repeating custom image field called 'featured-post-image' and two cred forms - one to create (ID=2973) and one to edit (ID=3121).

I tried many ideas... Am hoping you see where I am going wrong? 🙂 I think I am placing the $arr code wrong...

// LIMIT IMAGE SIZE FOR STORY POST TYPE IMAGE FIELD
add_filter('cred_form_validate','story_image_size_validation',10,2);
function story_image_size_validation($field_data, $form_data)
{
list($fields,$errors)=$field_data;
$arr = array(2973, 3121);
if (!in_array($form_data[id], $arr)){
if(isset($fields['wpcf-featured-post-image']['file_data']['size'])){
foreach($fields['wpcf-featured-post-image']['file_data']['size'] as $k => $v){
if($v>1000){
$errors['featured-post-image']='the image size should not be more than 1000 bytes';
}
}
}
}
return array($fields,$errors);
}

#288577

Luo Yang
Supporter

Languages: English (English ) Chinese (Simplified) (简体中文 )

Timezone: Asia/Hong_Kong (GMT+08:00)

Are you wordpress build-in feature image or custom image field "featured-post-image", if it is a custom image field created with Types, For the on-repeating custom image field, please try this:

add_filter('cred_form_validate','story_image_size_validation',10,2);
function story_image_size_validation($field_data, $form_data)
{
	list($fields,$errors)=$field_data;
	$arr = array(2973, 3121);
	if (!in_array($form_data[id], $arr)){
		if(isset($fields['wpcf-featured-post-image']['file_data']['size'])){
			if($fields['wpcf-featured-post-image']['file_data']['size']>1000){
				$errors['featured-post-image']='the image size should not be more than 1000 bytes';
			}
		}
	}
	return array($fields,$errors);
}
#288613

Morning Luoy
Yes, the 'featured-post-image' is a TYPES custom field for post type 'story' with two cred forms - one to create (2973) and one to edit (4098) - I had this form ID wrong and corrected it.
I added your code and...it doesn't work. I tried it on another post type and no... it doesn't work...
What do you reckon?
Thank you very much for your help on this!
I am sorry to be persistent. I really need this to work as I don't want to add yet another plugin to my site...

#288625

Luo Yang
Supporter

Languages: English (English ) Chinese (Simplified) (简体中文 )

Timezone: Asia/Hong_Kong (GMT+08:00)

Could you duplicate same problem in a test site, and fill below private detail box with login details, also point out the problem page URL and where I can edit the PHP codes. I need a live website to test and debug.

#288812

Luo Yang
Supporter

Languages: English (English ) Chinese (Simplified) (简体中文 )

Timezone: Asia/Hong_Kong (GMT+08:00)

Thanks for the details, I can log into your website, it is a mistake in my codes,
I have added below codes in your theme/functions.php:

//Limit upload file size for repeating image custom field
add_filter('cred_form_validate','story_image_size_validation',10,2);
function story_image_size_validation($field_data, $form_data)
{
    list($fields,$errors)=$field_data;
    $arr = array(2973, 4098);
    if (in_array($form_data[id], $arr)){
        if(isset($fields['wpcf-featured-post-image']['file_data']['size'])){
            if($fields['wpcf-featured-post-image']['file_data']['size']>1000){
                $errors['featured-post-image']='the image size should not be more than 1000 bytes';
            }
        }
    }
    return array($fields,$errors);
}

Please notice, it is:
if (in_array($form_data[id], $arr)){

Which means the custom function works only when the CRED form is 2973 or 4098

#291498

Thanks Luoy! All working nicely my end. Now just need to copy and paste for all the post types. 🙂