Sauter la navigation

[Résolu] Savig generic fields in a user form to a custom post

This support ticket is created Il y a 1 jour et 10 heures. 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 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

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

Dernière mise à jour par Timothy Il y a 5 heures et 18 minutes.

Assisté par: Minesh.

Auteur
Publications
#2809705

I have a custom post Private Teachers. I am trying to create a new user form that generates a new user with role Private Teacher and saves the form fields to a Private Teacher cpt. I thought I understood the process but only 2 fields are being saved to the new custom post (website and phone)

This is the form page:
lien caché

This is my form code:

[creduserform]
<div class="container-fluid">
	<div class="row">
		<div class="col-md-12">
			[cred_field field='form_messages' class='alert alert-warning']
		</div>
	</div>
	<div class="row">
	</div>
	<div class="row">
		<div class="form-group col-md-6">
			<label for="%%FORM_ID%%_first_name">[cred_i18n name='first_name-label']First name[/cred_i18n]</label>
			[cred_field field='first_name' class='form-control' output='bootstrap']
		</div>
		<div class="form-group col-md-6">
			<label for="%%FORM_ID%%_last_name">[cred_i18n name='last_name-label']Last name[/cred_i18n]</label>
			[cred_field field='last_name' class='form-control' output='bootstrap']
		</div>
	</div>
	<div class="row">
		<div class="form-group col-md-6">
			<label for="%%FORM_ID%%_user_email">[cred_i18n name='user_email-label']Email[/cred_i18n]</label>
			[cred_field field='user_email' class='form-control' output='bootstrap']
		</div>
		<div class="col-md-6">
			<label for="%%FORM_ID%%_phone">[cred_i18n name='phone-label']Phone[/cred_i18n]</label>
			[cred_generic_field type='phone' field='phone']
{
"required":0,
"default":""
}
[/cred_generic_field]
		</div>
	</div>
	<div class="row">
		<div class="form-group col-md-12">
			<label for="%%FORM_ID%%_user_pass">[cred_i18n name='user_pass-label']Login Password[/cred_i18n]</label>
			[cred_field field='user_pass' class='form-control' output='bootstrap']
<label for="%%FORM_ID%%_user_pass2">[cred_i18n name='user_pass2-label']Repeat Password[/cred_i18n]</label>[cred_field field='user_pass2' class='form-control' output='bootstrap']
		</div>
	</div>
	<div class="row">
		<div class="col-md-12">
			<label for="%%FORM_ID%%_your-website">[cred_i18n name='your-website-label']Your Website[/cred_i18n]</label>
			[cred_generic_field type='url' field='your-website']
{
"required":0,
"default":""
}
[/cred_generic_field]
		</div>
	</div>
	<div class="row">
		<div class="col-md-12">
			<label for="%%FORM_ID%%_profile-bio">[cred_i18n name='profile-bio-label']Profile Bio[/cred_i18n]</label>
			[cred_generic_field type='textarea' field='profile-bio']
{
"required":1,
"default":"1"
}
[/cred_generic_field]
		</div>
	</div>
	<div class="row">
		<div class="col-md-6">
			<label for="%%FORM_ID%%_teaching-location">[cred_i18n name='teaching-location-label']Teaching Location[/cred_i18n]</label>
			[cred_generic_field type='checkboxes' field='teaching-location']
{
"default":[],
"options":[{"value":"Richmond","label":"Richmond"},{"value":"Chesterfield","label":"Chesterfield"},{"value":"Hanover","label":"Hanover"},{"value":"Henrico","label":"Henrico"},{"value":"Northern Virginia","label":" Northern Virginia"},{"value":"New York City","label":"New York City"}]
}
[/cred_generic_field]
		</div>
		<div class="col-md-6">
			<label for="%%FORM_ID%%_teaching-modality">[cred_i18n name='teaching-modality-label']Teaching Modality[/cred_i18n]</label>
			[cred_generic_field type='checkboxes' field='teaching-modality']
{
"default":[],
"options":[{"value":"In-person (at my studio)  ","label":"In-person (at my studio)  "},{"value":"In-person (at students homes)","label":"In-person (at students homes)"},{"value":"Online","label":"Online"},{"value":" At the music school I also teach at","label":" At the music school I also teach at"}]
}
[/cred_generic_field]
		</div>
	</div>
	<div class="row">
		<div class="col-md-12">
			<label for="%%FORM_ID%%_instrument-teaching">[cred_i18n name='instrument-teaching-label']Instrument Teaching[/cred_i18n]</label>
			[cred_generic_field type='checkboxes' field='instrument-teaching']
{
"default":[],
"options":[{"value":"Bass Clarinet","label":"Bass Clarinet"},{"value":"Bassoon","label":"Bassoon"},{"value":"Clarinet","label":"Clarinet"},{"value":"Cello","label":"Cello"},{"value":"Double Bass","label":"Double Bass"},{"value":"English Horn","label":"English Horn"},{"value":"Flute","label":"Flute"},{"value":"Harp","label":"Harp"},{"value":"Horn","label":"Horn"},{"value":"Music Theory","label":"Music Theory"},{"value":"Oboe","label":"Oboe"},{"value":"Percussion","label":"Percussion"},{"value":"Piano","label":"Piano"},{"value":"Piccolo","label":"Piccolo"},{"value":"Saxophone","label":"Saxophone"},{"value":"Trombone","label":"Trombone"},{"value":"Trumpet","label":"Trumpet"},{"value":"Tuba","label":"Tuba"},{"value":"Viola","label":"Viola"},{"value":"Violin","label":"Violin"}]
}
[/cred_generic_field]
		</div>
	</div>
	<div class="row">
		<div class="col-md-4">
			<label for="%%FORM_ID%%_instrument-family">[cred_i18n name='instrument-family-label']Instrument Family[/cred_i18n]</label>
			[cred_generic_field type='checkboxes' field='instrument-family']
{
"default":[],
"options":[{"value":"Woodwinds","label":"Woodwinds"},{"value":"Brass","label":"Brass"},{"value":"Percussion/Timpani","label":"Percussion/Timpani"},{"value":"Strings","label":"Strings"},{"value":"Harp","label":"Harp"},{"value":"Piano","label":"Piano"}]
}
[/cred_generic_field]
		</div>
		<div class="col-md-4">
			<label for="%%FORM_ID%%_ages-taught">[cred_i18n name='ages-taught-label']Ages Taught[/cred_i18n]</label>
			[cred_generic_field type='checkboxes' field='ages-taught']
{
"default":[],
"options":[{"value":"Preschool and younger       ","label":"Preschool and younger       "},{"value":"Elementary school","label":"Elementary school"},{"value":"Middle school","label":"Middle school"},{"value":"High school","label":"High school"},{"value":"Collegiate","label":"Collegiate"},{"value":"Adult learners","label":"Adult learners"},{"value":"All Ages","label":"All Ages"}]
}
[/cred_generic_field]
		</div>
		<div class="col-md-4">
			<label for="%%FORM_ID%%_experience-level-taught">[cred_i18n name='experience-level-taught-label']Experience Level Taught  [/cred_i18n]</label>
			[cred_generic_field type='checkboxes' field='experience-level-taught']
{
"default":[],
"options":[{"value":"Beginners","label":"Beginners"},{"value":"Intermediate","label":"Intermediate"},{"value":"Advanced","label":"Advanced"},{"value":"Pre-professional","label":"Pre-professional"}]
}
[/cred_generic_field]
		</div>
	</div>
	<div class="row">
		<div class="col-md-12">
			<label for="%%FORM_ID%%_private-teacher-photo">[cred_i18n name='private-teacher-photo-label']Your Photo[/cred_i18n]</label>
			[cred_generic_field type='image' field='private-teacher-photo']
{
"required":0
}
[/cred_generic_field]
		</div>
	</div>
	<div class="row">
		<div class="col-md-6">
			<label for="%%FORM_ID%%_teaching-certificate-1">[cred_i18n name='teaching-certificate-1-label']Teaching Certificate 1[/cred_i18n]</label>
			[cred_generic_field type='textfield' field='teaching-certificate-1']
{
"required":0,
"default":""
}
[/cred_generic_field]
		</div>
		<div class="col-md-6">
			<label for="%%FORM_ID%%_teaching-certificate-2">[cred_i18n name='teaching-certificate-2-label']Teaching Certificate 2[/cred_i18n]</label>
			[cred_generic_field type='textfield' field='teaching-certificate-2']
{
"required":0,
"default":""
}
[/cred_generic_field]
		</div>
	</div>
	<div class="row">
		<div class="col-md-12">
			[cred_field field='form_submit' output='bootstrap' value='Submit' class='btn btn-primary btn-lg']
		</div>
	</div>
</div>
[/creduserform]

And I added this code in my functions file:

// Saving Cred Generic Fields Data for private teacher registration form
add_action('cred_save_data', 'my_save_data_action_teacher',10,2);
function my_save_data_action_teacher($post_id, $form_data)
{
    if ($form_data['id']==9210)
    {
        // Create post object
        $new_post_id = wp_insert_post( array(
            'post_title'    => $_POST['user_firstname'],
            'post_content'  => '',
            'post_status'   => 'publish',
            'post_author'   => $post_ID,
            'post_type'     => 'private-teacher'
        ) );
		update_post_meta($new_post_id, 'wpcf-wpv-post-body', $_POST['profile-bio']);
        update_post_meta($new_post_id, 'wpcf-private-teacher-instrument', $_POST['instrument-teaching']);
        update_post_meta($new_post_id, 'wpcf-private-teacher-instrument-family', $_POST['instrument-family']);
        update_post_meta($new_post_id, 'wpcf-private-teacher-email', $_POST['email']);
		update_post_meta($new_post_id, 'wpcf-private-teacher-phone', $_POST['phone']);
        update_post_meta($new_post_id, 'wpcf-private-teacher-website', $_POST['your-website']);
        update_post_meta($new_post_id, 'wpcf-private-teacher-ages-taught', $_POST['ages-taught']);
		update_post_meta($new_post_id, 'wpcf-private-teacher-experience-level', $_POST['experience-level-taught']);
        update_post_meta($new_post_id, 'wpcf-private-teacher-course-modality', $_POST['teaching-modality']);
        update_post_meta($new_post_id, 'wpcf-private-teacher-location', $_POST['teaching-location']);
        update_post_meta($new_post_id, 'wpcf-private-teacher-teaching-certifications', $_POST['teaching-certificate-1']);


        // get image URL value from the user
        $user_image = get_user_meta( $post_id, 'wpcf-private-teacher-photo', true );
        // if image URL exists
        if (!empty($user_image)) {
            // set the image URL as new post's custom field value
            update_post_meta($new_post_id, 'wpcf-private-teacher-photo', $user_image);
            // delete the image URL from user's custom field
            delete_user_meta($post_id, 'wpcf-private-teacher-photo');
        }
    }
}
#2809706

Minesh
Supporter

Les langues: Anglais (English )

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

Hello. Thank you for contacting the Toolset support.

I see with the code you shared, with the wp_insert_post() function you are passing post author as given under:

'post_author' => $post_ID,

But I do not see variable $post_ID, maybe you want to change the post authour to current loggedin user id?

And also, instead of using "cred_save_data" hook, what if you try to use the Toolset Forms hook "cred_submit_complete":
=> https://toolset.com/documentation/programmer-reference/cred-api/#cred_submit_complete

#2809711

I just tried with cred_submit_complete and it still does not work. Can you clarify "maybe you want to change the post authour to current loggedin user id?" How do I do this?

#2809712

Minesh
Supporter

Les langues: Anglais (English )

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

Let me check why its not working.

Can you please send me admin access details and also tell me where exactly you added the code you shared?

*** 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.

#2809720

Minesh
Supporter

Les langues: Anglais (English )

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

I see you have multiple checkboxes fields and to update the checkboxes fields using PHP. Checkboxes are special fields that stores its value to database table as serialize array.

First of all, I've removed "cred_submit_complete" hook yu added to your child theme's functions.php file and I moved that code to "Custom Code" section offered by Toolset with the code snippet namely "update-checkboxes-field-php":
=> lien caché

Here is the function that should help to update the checkboxes field value to database using PHP:

function ts_checkboxes( $post_id, $field, $option, $action = 'value' ){
 
    if ( isset($post_id) && isset($field) && isset($option) ){
 
        $field_settings = types_get_field( $field );
         
        $field_options = $field_settings['data']['options'];
 
        // Locate the option key
        $key = array_search( $option, array_column( $field_options, 'title' ) );
        $keys = array_keys( $field_options );
        $option_key = $keys[$key];
 
      
        // Get the current post meta value
        $meta = get_post_meta( $post_id, 'wpcf-'.$field, true );
 
        // If action = 'value' just return the value
        if ( $action == 'value' && isset( $meta[$option_key] ) ){
            return $meta[$option_key][0];
        } else {
            // Remove the existing key if it exists (i.e. uncheck)
            // because recommended setting is to save nothing when unchecked
            unset( $meta[$option_key] );
             
            // If $checked == true then add back the key + value
            if ( $action == 'check' ){
                $meta[$option_key] = array($field_options[$option_key]['set_value']);
            }
            update_post_meta( $post_id, 'wpcf-'.$field, $meta );        
        }
    }
}

And I've called the above function to update the checkboxes fields as you can see with the following hook code:

// Saving Cred Generic Fields Data for private teacher registration form
add_action('cred_submit_complete', 'my_save_data_action_teacher',10,2);
function my_save_data_action_teacher($post_id, $form_data)
{
    if ($form_data['id']==9210)
    {
        // Create post object
        $new_post_id = wp_insert_post( array(
            'post_title'    => $_POST['user_firstname'],
            'post_content'  => '',
            'post_status'   => 'publish',
          //  'post_author'   => $post_ID,
            'post_type'     => 'private-teacher'
        ) );
      
      
      ///// update checkboxes fields based on select values php 
      if(isset($_POST['teaching-location']) and !empty($_POST['teaching-location'])) {
      		foreach($_POST['teaching-location'] as $k=>$v):
        		ts_checkboxes( $post_id, 'private-teacher-location', $v, 'check' );
        	endforeach;
      }else{
      	update_post_meta($post_id, 'wpcf-private-teacher-location', array());
      }
      
       if(isset($_POST['teaching-modality']) and !empty($_POST['teaching-modality'])) {
      		foreach($_POST['teaching-modality'] as $k=>$v):
        		ts_checkboxes( $post_id, 'private-teacher-course-modality', $v, 'check' );
        	endforeach;
      }else{
      	update_post_meta($post_id, 'wpcf-private-teacher-course-modality', array());
      }
      
       if(isset($_POST['experience-level-taught']) and !empty($_POST['experience-level-taught'])) {
      		foreach($_POST['experience-level-taught'] as $k=>$v):
        		ts_checkboxes( $post_id, 'private-teacher-experience-level', $v, 'check' );
        	endforeach;
      }else{
      	update_post_meta($post_id, 'wpcf-private-teacher-experience-level', array());
      }
      
       if(isset($_POST['instrument-teaching']) and !empty($_POST['instrument-teaching'])) {
      		foreach($_POST['instrument-teaching'] as $k=>$v):
        		ts_checkboxes( $post_id, 'private-teacher-instrument', $v, 'check' );
        	endforeach;
      }else{
      	update_post_meta($post_id, 'wpcf-private-teacher-instrument', array());
      }
      
      if(isset($_POST['instrument-family']) and !empty($_POST['instrument-family'])) {
      		foreach($_POST['instrument-family'] as $k=>$v):
        		ts_checkboxes( $post_id, 'private-teacher-instrument-family', $v, 'check' );
        	endforeach;
      }else{
      	update_post_meta($post_id, 'wpcf-private-teacher-instrument-family', array());
      }
      
       if(isset($_POST['ages-taught']) and !empty($_POST['ages-taught'])) {
      		foreach($_POST['ages-taught'] as $k=>$v):
        		ts_checkboxes( $post_id, 'private-teacher-ages-taught', $v, 'check' );
        	endforeach;
      }else{
      	update_post_meta($post_id, 'wpcf-private-teacher-ages-taught', array());
      }
      
       if(isset($_POST['experience-level-taught']) and !empty($_POST['experience-level-taught'])) {
      		foreach($_POST['experience-level-taught'] as $k=>$v):
        		ts_checkboxes( $post_id, 'private-teacher-experience-level', $v, 'check' );
        	endforeach;
      }else{
      	update_post_meta($post_id, 'wpcf-private-teacher-experience-level', array());
      }
      
		update_post_meta($new_post_id, 'wpcf-wpv-post-body', $_POST['profile-bio']);
        update_post_meta($new_post_id, 'wpcf-private-teacher-email', $_POST['email']);
		update_post_meta($new_post_id, 'wpcf-private-teacher-phone', $_POST['phone']);
        update_post_meta($new_post_id, 'wpcf-private-teacher-website', $_POST['your-website']);
        update_post_meta($new_post_id, 'wpcf-private-teacher-teaching-certifications', $_POST['teaching-certificate-1']);


        // get image URL value from the user
        $user_image = get_user_meta( $post_id, 'wpcf-private-teacher-photo', true );
        // if image URL exists
        if (!empty($user_image)) {
            // set the image URL as new post's custom field value
            update_post_meta($new_post_id, 'wpcf-private-teacher-photo', $user_image);
            // delete the image URL from user's custom field
            delete_user_meta($post_id, 'wpcf-private-teacher-photo');
        }
    }
}

Can you please confirm it works as expected now.

#2809744

That gave me the "there has been a critical error on this site" page, but the site is fine overall. And it did generate a new cpt and wp user, but no fields were saved.

lien caché

Perhaps I should create a staging site for this?

Tim

#2809745

Minesh
Supporter

Les langues: Anglais (English )

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

We have to use the $new_post_id instead of the $post_id to update the checkboxes field.

So I've adjusted $new_post_id as given under, for example:

ts_checkboxes( $new_post_id, 'private-teacher-location', $v, 'check' );

Here is the full code:

add_action('cred_submit_complete', 'my_save_data_action_teacher',10,2);
function my_save_data_action_teacher($post_id, $form_data) {
  
    if ($form_data['id']==9210) {
        // Create post object
        $new_post_id = wp_insert_post( array(
            'post_title'    => $_POST['user_firstname'],
            'post_content'  => '',
            'post_status'   => 'publish',
          //  'post_author'   => $post_ID,
            'post_type'     => 'private-teacher'
        ) );
      
      
      ///// update checkboxes fields 
      if(isset($_POST['teaching-location']) and !empty($_POST['teaching-location'])) {
      		foreach($_POST['teaching-location'] as $k=>$v):
        		ts_checkboxes( $new_post_id, 'private-teacher-location', $v, 'check' );
        	endforeach;
      }else{
      	update_post_meta($new_post_id, 'wpcf-private-teacher-location', array());
      }
      
       if(isset($_POST['teaching-modality']) and !empty($_POST['teaching-modality'])) {
      		foreach($_POST['teaching-modality'] as $k=>$v):
        		ts_checkboxes( $new_post_id, 'private-teacher-course-modality', $v, 'check' );
        	endforeach;
      }else{
      	update_post_meta($new_post_id, 'wpcf-private-teacher-course-modality', array());
      }
      
       if(isset($_POST['experience-level-taught']) and !empty($_POST['experience-level-taught'])) {
      		foreach($_POST['experience-level-taught'] as $k=>$v):
        		ts_checkboxes( $new_post_id, 'private-teacher-experience-level', $v, 'check' );
        	endforeach;
      }else{
      	update_post_meta($new_post_id, 'wpcf-private-teacher-experience-level', array());
      }
      
       if(isset($_POST['instrument-teaching']) and !empty($_POST['instrument-teaching'])) {
      		foreach($_POST['instrument-teaching'] as $k=>$v):
        		ts_checkboxes( $new_post_id, 'private-teacher-instrument', $v, 'check' );
        	endforeach;
      }else{
      	update_post_meta($new_post_id, 'wpcf-private-teacher-instrument', array());
      }
      
      if(isset($_POST['instrument-family']) and !empty($_POST['instrument-family'])) {
      		foreach($_POST['instrument-family'] as $k=>$v):
        		ts_checkboxes( $new_post_id, 'private-teacher-instrument-family', $v, 'check' );
        	endforeach;
      }else{
      	update_post_meta($new_post_id, 'wpcf-private-teacher-instrument-family', array());
      }
      
       if(isset($_POST['ages-taught']) and !empty($_POST['ages-taught'])) {
      		foreach($_POST['ages-taught'] as $k=>$v):
        		ts_checkboxes( $new_post_id, 'private-teacher-ages-taught', $v, 'check' );
        	endforeach;
      }else{
      	update_post_meta($new_post_id, 'wpcf-private-teacher-ages-taught', array());
      }
      
       if(isset($_POST['experience-level-taught']) and !empty($_POST['experience-level-taught'])) {
      		foreach($_POST['experience-level-taught'] as $k=>$v):
        		ts_checkboxes( $new_post_id, 'private-teacher-experience-level', $v, 'check' );
        	endforeach;
      }else{
      	update_post_meta($new_post_id, 'wpcf-private-teacher-experience-level', array());
      }
      
		update_post_meta($new_post_id, 'wpcf-wpv-post-body', $_POST['profile-bio']);
        update_post_meta($new_post_id, 'wpcf-private-teacher-email', $_POST['email']);
		update_post_meta($new_post_id, 'wpcf-private-teacher-phone', $_POST['phone']);
        update_post_meta($new_post_id, 'wpcf-private-teacher-website', $_POST['your-website']);
        update_post_meta($new_post_id, 'wpcf-private-teacher-teaching-certifications', $_POST['teaching-certificate-1']);


        // get image URL value from the user
        $user_image = get_user_meta( $post_id, 'wpcf-private-teacher-photo', true );
        // if image URL exists
        if (!empty($user_image)) {
            // set the image URL as new post's custom field value
            update_post_meta($new_post_id, 'wpcf-private-teacher-photo', $user_image);
            // delete the image URL from user's custom field
            delete_user_meta($post_id, 'wpcf-private-teacher-photo');
        }
    }
}

Can you please check now, it should work.

#2809751

Same result, critical error and no data saved.

#2809848

Minesh
Supporter

Les langues: Anglais (English )

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

This is really strange.

Can you please setup a staging site and send me admin access details and let me review what's going wrong with your setup.

*** 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.

#2809872

Minesh
Supporter

Les langues: Anglais (English )

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

The issue was with the checkboxes field "Private Teacher Course Modality" you have setup the one of the option text with apostrophe, for example "In-person (at students' homes)"
=> lien caché

So I've removed that aphostrophe in the option, it looks like now: "In-person (at students homes)"

And save the above custom field group.

Then I've also modified the code and add to "Custom Code" section with the code snippet namely " update-checkboxes-field-php" as given under:
=> lien caché

//// 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_content'  => '',
            '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']
                )
        ) );
  
    
    
}
}

You should make sure to map all your fields that you want to save correctly with your post type and generic field.

Please let me know if you require further help, if yes, I will have to split this ticket.

#2809904

Excellent, yes this works now. Thank you. I should have thought about that apostrophe before. The only other issue is the field for Teaching Certificates, which in toolset I have as a single line field that allows multiple instances. Is it possible to handle this with generic fields? Or should I create a separate ticket for this issue?

Tim

#2809905

Minesh
Supporter

Les langues: Anglais (English )

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

Glad to know that the solution I shared help you to resolve the issue of updating checkboxes field value for selected options using generic field using PHP.

You're welcome to mark resolve this ticket and please open a new ticket with every new question you may have. This will help other users searching on the forum as well as help us to write correct problem resolution summery for the original issue reported.

#2809909

Thank you. I will open a new ticket for the other question.