Skip Navigation

[Resolved] Php function for limiting size of images uploaded NOT working

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to use the Forms API to validate each image that is uploaded in a single or repeating custom field. It doesn't seem to be working, in the logs I see a PHP notice about trying to get property of a non-object.

Solution: Make sure the User has permission to publish Media if you are using Toolset Access Control. Make sure the Form's markup structure doesn't include any extra closing div tags. Use the following custom code to validate a repeating image field:

add_filter('cred_form_ajax_upload_validate','rep_img_size_validation',10,2);
function rep_img_size_validation($error_fields, $form_data)
{
error_log(' validation script is invoked ');
  $forms = array( 29, 49 );
  $size = 1024000;
  $field_slug = 'your-repeating-image-slug';
  $error_msg = 'Maximum size is 1MB, please try again.';
  
  //field data are field values and errors
  list($fields,$errors)=$error_fields;
  //validate if specific form
  if (in_array( $form_data['id'], $forms ) && isset($fields['wpcf-' . $field_slug]['field_data']['size']))
  {
    //check if this field instance img is bigger than $size (bytes)
    $instance_size = array_pop($fields['wpcf-' . $field_slug]['field_data']['size']);
    if ( $instance_size > $size )
    {  
      //display error message for this field instance
      $errors['wpcf-' . $field_slug] = $error_msg;  
    }
  }
  //return result
  return array($fields,$errors);
}

To validate a single field, copy the code above and make the following adjustments:
- Change the function names to prevent a PHP error
- Change the field slug
- Remove array_pop from the instance_size definition like this:

// for repeating images
// $instance_size = array_pop($fields['wpcf-' . $field_slug]['field_data']['size']);
// for single images
$instance_size = $fields['wpcf-' . $field_slug]['field_data']['size'];

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_ajax_upload_validate

This support ticket is created 5 years, 7 months ago. 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.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Author
Posts
#1235734
Screen Shot 2019-04-25 at 12.07.28 PM.png

The User you are testing with is an Author. The Author role does not have permission to publish Media, so you cannot upload images in Forms. You must modify the Access Control permissions for the Media post type if you want to allow Authors to upload images.

#1235753

Duhhh, I'm sorry Christian, I probably drove you crazy there for a second. I totally forgot that I block everything for everyone in Access. It's working now!!

Now, how can I do the same for the single custom image? Do I duplicate the function and change the slug?

#1235756

No problem, that was easy to overlook. To validate the single image field you will duplicate the code, then make a few changes:
- Change the function names to prevent a PHP error
- Change the field slug
- Remove array_pop from the instance_size definition like this:

// for repeating images
// $instance_size = array_pop($fields['wpcf-' . $field_slug]['field_data']['size']);
// for single images
$instance_size = $fields['wpcf-' . $field_slug]['field_data']['size'];
#1235762

My issue is resolved now. Thank you!