Skip Navigation

[Resolved] Limiting the amount of images that can be uploaded in a Post Forms.

This support ticket is created 6 years, 5 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)

This topic contains 1 reply, has 2 voices.

Last updated by Christian Cox 6 years, 5 months ago.

Assisted by: Christian Cox.

Author
Posts
#1113815

Hello Toolset Support Team,

We are using your Classifieds Reference Site and we are customizing your ad packages. We want to limit the amount of images for the 'Free Package' to 3 images.

If you can help us come up with a solution, we would be very appreciative.

Thank you,
JunkPal Development

#1113915

Hi, the code in the Classifieds plugin isn't written to be easily extensible, and adding new packages or modifying existing packages is not recommended. We don't recommend modifying the functionality of the Classifieds Reference site, and it is not meant to be used as a boilerplate for creating your own classifieds site. It's meant to show what is possible with Toolset and custom code.

With that being said, in general you can use the Forms API cred_form_validate to validate image uploads when AJAX is not used on the Form. Add this code to your child theme's functions.php file:

add_filter("cred_file_upload_disable_progress_bar", "disable_progress_bar");
function disable_progress_bar($val) {
   return true; //return true to disable
}

function not_empty($var) {
  return $var != '';
}

add_action('cred_form_validate', 'toolset_repeating_image_count_restriction',10,2);
function toolset_repeating_image_count_restriction($error_fields, $form_data) {
  $forms = array( 12345 );
  // Field data are field values and errors
  list($fields,$errors) = $error_fields;
  if (in_array($form_data['id'], $forms ) && isset($_FILES['wpcf-rept-img']['tmp_name']) ) {
    $not_empty = array_filter($_FILES['wpcf-rept-img']['tmp_name'], 'not_empty');
    if(count($not_empty) > 3) {
      $errors['rept-img'] = __("Maximum of 3 images allowed", "your-theme-domain");
    }
  }
  $field_data = array($fields,$errors);
  //return result
  return $field_data;
}

Change 12345 to match the Form's numeric ID, change rept-img to match the repeating image field slug, and change the error message as needed.