Quantcast
Channel: Toolset » All Posts
Viewing all articles
Browse latest Browse all 20145

Reply To: [Waiting for user feedback] Change image filename after upload via CRED form

$
0
0

Hi,

thanks for the fast reply. I tried both and failed somehow…

i tried the last one and changed the values like you mentioned. The ID of the CRED form is 2363.

add_filter('cred_form_validate', 'my_validation', 10, 2);
function my_validation($field_data, $form_data) {
 // field data are field values and errors
 list($fields,$errors)=$field_data;
 // validate if specific form
 if ($form_data['id']==2363){
  global $post;
  //configure image field names to change
  $custom_fields = array('titelbild', 'bild1');
  if (isset($post->ID)){
   foreach ($custom_fields as $field_name){
    if (isset($fields[$field_name]['value'])){
     //check if one or multi-instance field
     if (isset($fields[$field_name]['value'][0])){
      //multi-instance
      foreach ($fields[$field_name]['value'] as $key=>$image){
       if (isset($image['value']) && isset($image['file_data']['wpcf-'. $field_name]['name'])){
        $fields[$field_name]['value'][$key]['value'] = $fields[$field_name]['value'][$key]['file_data']['wpcf-'. $field_name]['name'] = $post->ID . '-' . $image['value'];
       }
      }
     } else {
      //one value
      if (isset($fields[$field_name]['value']['value']) && isset($fields[$field_name]['value']['file_data']['wpcf-'. $field_name]['name'])){
       $fields[$field_name]['value']['value'] = $fields[$field_name]['value']['file_data']['wpcf-'. $field_name]['name'] = $post->ID . '-' . $fields[$field_name]['value']['value'];
      }
     }
    }
   }
  }
 }
 // return result
 return array($fields,$errors);
}

This is one of my fields for the image

[cred_field field="bild1" post="obj" value="" max_width="1200" max_height="1200"]

Viewing all articles
Browse latest Browse all 20145

Trending Articles