Passer la navigation

[Résolu] Saving Generic Image and Repeatable Text Fields to a Custom Post

This support ticket is created Il y a 1 mois. 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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Fuseau horaire du supporter : Asia/Kolkata (GMT+05:30)

Ce sujet contient 8 réponses, a 1 voix.

Dernière mise à jour par Minesh Il y a 2 semaines et 6 jours.

Assisté par: Minesh.

Auteur
Publications
#2812492

This is a follow on to my previous ticker here:
https://toolset.com/forums/topic/savig-generic-fields-in-a-user-form-to-a-custom-post/

This works for all the fields except the image field, the Single Line fields with multiple instances (Teaching Certificate and Music Schools Taught At), and the main content editor field(Teacher Profile). Is there something special I need to do to get these fields to save from the generic fields to the CPT?

Tim

#2812503

Minesh
Supporter

Les langues: Anglais (English )

Fuseau horaire: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Can you please share the problem URL where you added the form as well as admin access details and also tell me what generic field you want to connect with what actual custom field you created with the Toolset Types plugin.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2813178

Minesh
Supporter

Les langues: Anglais (English )

Fuseau horaire: Asia/Kolkata (GMT+05:30)

When I check the private teacher post in backend admin:
=> lien caché

I see the field and field type are different and that's bit confusing.

Can you please share details what generic field you want to bind with what actual private teacher field?

Like:
- Generic field X value should be saved to X actual private teacher post field.

#2813313

I think you were looking at the teacher edit form, not the add new teacher form, which is here:
lien caché

And here is what I want to bind:

Generic field private-teacher-photo should be saved to private-teacher-photo.

Generic field profile-bio should be saved to the main content editor field.

Generic field teaching-certificate-1 should be saved to private-teacher-teaching-certifications ( A single line field with multiple instances) the first instance.

Generic field teaching-certificate-2 should be saved to private-teacher-teaching-certifications ( A single line field with multiple instances) the second instance.

Generic field music-school-taught-at-1 should be saved to private-teacher-music-schools-taught-at ( A single line field with multiple instances) the first instance.

Generic field music-school-taught-at-2 should be saved to private-teacher-music-schools-taught-at ( A single line field with multiple instances) the second instance.

For the Edit Teacher form, will I have to create another code snippet to save things?

Tim

#2813801

Minesh
Supporter

Les langues: Anglais (English )

Fuseau horaire: Asia/Kolkata (GMT+05:30)

Can you please check now.

I've adjusted the code added to "Custom Code" section with code snippet namely "toolset-custom-code" as given under:

//// function to retrive the selected checkboxes options to save with php
function get_selected_checkboxes_options($generic_field_slug,$custom_field_slug) {

  	   $field_settings = types_get_field( $custom_field_slug );
       $field_options = $field_settings['data']['options'];
 	   $all_option_keys = array_keys( $field_options );
       
      $selected_options = array();
      if(isset($_POST[$generic_field_slug])) {
        
      		foreach($_POST[$generic_field_slug] as $k=>$v):
        		
        		$found_key = array_search( $v, array_column( $field_options, 'title' ) );
        
$selected_options[$all_option_keys[$found_key]] = array($field_options[$all_option_keys[$found_key]]['set_value']);
      
        	  
        	endforeach;
        }
  		return $selected_options;
  
}


add_action('cred_save_data', 'func_add_new_item_and_update_checkboxes',10,2);
function func_add_new_item_and_update_checkboxes($post_id, $form_data) {
      
  $allowed_form_ids = array( 9210 );
     
  if ( in_array( $form_data['id'], $allowed_form_ids ) ) {
       
                $current_user_id = get_current_user_id();
    
    	/////// getting the checkboxes fields selected options 
              $private_teacher_location = get_selected_checkboxes_options('teaching-location','private-teacher-location');
       $private_teacher_course_modality = get_selected_checkboxes_options('teaching-modality','private-teacher-course-modality');
      
     
       $private_teacher_instrument = get_selected_checkboxes_options('instrument-teaching','private-teacher-instrument');
      
       $private_teacher_instrument_family = get_selected_checkboxes_options('instrument-family','private-teacher-instrument-family');
      
         $private_teacher_ages_taught = get_selected_checkboxes_options('ages-taught','private-teacher-ages-taught');
      
       $private_teacher_experience_level = get_selected_checkboxes_options('experience-level-taught','private-teacher-experience-level');
      
      
       // Create post object
     $new_post_id = wp_insert_post( array(
            'post_title'    => $_POST['first_name'] . ' ' . $_POST['last_name'],
            'post_content'  => $_POST['profile-bio'],
            'post_status'   => 'publish',
            'post_author'   => $post_id,
            'post_type'     => 'private-teacher',
          	'meta_input'     => array(
                    'wpcf-private-teacher-location' => $private_teacher_location,
              		'wpcf-private-teacher-course-modality' => $private_teacher_course_modality,
              		'wpcf-private-teacher-instrument' => $private_teacher_instrument,
              		'wpcf-private-teacher-instrument-family' => $private_teacher_instrument_family,
              		'wpcf-private-teacher-ages-taught' => $private_teacher_ages_taught,
              		'wpcf-private-teacher-experience-level' => $private_teacher_experience_level,
                    'wpcf-private-teacher-email' => $_POST['user_email'],
              		'wpcf-private-teacher-phone'=> $_POST['phone'],
              		'wpcf-private-teacher-website' => $_POST['your-website'],
                    //'wpcf-private-teacher-photo' => $_POST['private-teacher-photo'],
                    //'wpcf-private-teacher-teaching-certifications' => $_POST['teaching-certificate-1'],
                    // 'wpcf-private-teacher-music-schools-taught-at' => $_POST['private-teacher-music-schools-taught-at']

                )
        ) );
    
    add_post_meta($new_post_id,'wpcf-private-teacher-teaching-certifications',$_POST['teaching-certificate-1']);
     add_post_meta($new_post_id,'wpcf-private-teacher-teaching-certifications',$_POST['teaching-certificate-2']);
    
    add_post_meta($new_post_id,'wpcf-private-teacher-music-schools-taught-at',$_POST['music-school-taught-at-1']);
     add_post_meta($new_post_id,'wpcf-private-teacher-music-schools-taught-at',$_POST['music-school-taught-at-2']);
    
    
    
          
    
    	////// uploading generic image field.
    	$image_file_slug = 'private-teacher-photo';
    	$attachment_id = save_generic_image_field_to_media($image_file_slug,$new_post_id);
    	 if ($attachment_id) {
                    // Get the URL of the uploaded file
                    $file_url = wp_get_attachment_url($attachment_id);
           			update_post_meta($new_post_id, 'wpcf-'.$image_file_slug, $file_url);
         } 
    
    
    
}
}

//// function to upldate generic field image to media library 
function save_generic_image_field_to_media($image_or_file_field_slug,$attach_to_post_id) {

    	if (!empty($_FILES[$image_or_file_field_slug]['name'])) {

            require_once(ABSPATH . 'wp-admin/includes/file.php');
            require_once(ABSPATH . 'wp-admin/includes/media.php');
            require_once(ABSPATH . 'wp-admin/includes/image.php');

            $file = $_FILES[$image_or_file_field_slug];

            // Proceed only if no upload error
            if ($file['error'] === 0) {
                $attachment_id = media_handle_upload($image_or_file_field_slug, $attach_to_post_id);
				return $attachment_id;

            } else {
                error_log('File upload error code: ' . $file['error']);
				return false;
            }
        }
}

Can you please confirm it works as expected.

#2813812

Thanks! Everything worked except the photo field, there was no error message and the image did not get saved. But I asked chatgpt why this might be and incredibly it told me to just add "<input type="file" name="private-teacher-photo" />" to the image generic field and it now works. The below is for reference in case anyone else needs this.

[cred_generic_field type='image' field='private-teacher-photo']
{
"required":0
}
<input type="file" name="private-teacher-photo" />
[/cred_generic_field]

#2813832

Minesh
Supporter

Les langues: Anglais (English )

Fuseau horaire: Asia/Kolkata (GMT+05:30)

It should be working without you add the <input type="file" name="private-teacher-photo" />.

Please try to clear the browser/server cache and check.

#2814115

well, it works now, any reason it might not be good to add that?

#2814260

Minesh
Supporter

Les langues: Anglais (English )

Fuseau horaire: Asia/Kolkata (GMT+05:30)

Weel - its not required 🙂 when you add Generic field field.

Maybe before it was not working due to the cache. Please make sure to clear server/browser cache.