Skip Navigation

[Resolved] Saving Multiple Checkboxes doesn’t work

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

Problem:

The issue here is that the user was trying to save their checkbox group using PHP.

Solution:
Unfortunately this is not an easy solutions and I would recommend not doing this.

My recommendation can be seen below.
https://toolset.com/forums/topic/saving-multiple-checkboxes-doesnt-work/#post-1199322

This support ticket is created 5 years, 9 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
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 12 replies, has 2 voices.

Last updated by Timothy 5 years, 9 months ago.

Assisted by: Shane.

Author
Posts
#1197633

I am trying to:

To save the custom multiple checkboxes I created using this code

add_action('cred_save_data', 'save_coach_cpt_init',10,2);

function save_coach_cpt_init($post_id, $form_data) {
update_post_meta($post_id, 'wpcf-coach-specialties-receiver', $_POST['coach-specialties']);
}

Link to a page where the issue can be seen:

hidden link see section your specialties.

I expected to see:

After submission it don't get Checkboxes checked on the Post Item in the backend hidden link

#1197844

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Timothy,

Thank you for contacting our support.

The checkbox fields are stored a little differently from regular custom field, so this method won't work.

Is there a reason why you are using a hook to set the value of the field ?

Thanks,
Shane

#1198085

Hello, yes because I want to submit the the form with the automatic creation of Custom Post type with the save Custom field on it. What's the code for this when the Checkboxes are checked on the Form and then it will be automatically checked in the backend of custom post type item.

#1198411

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Timothy,

If the fields are checked on the form itself when it is submitted then there is no need to use a hook to save it.

Is it that this field is not saving on the form ? Please let me know the scenario as I may not fully understand the reason behind this.

Looking forward to hearing from you.

Thanks,
Shane

#1198573

Sorry, I think I didn't explain well. I have a User Form where I am registering a user as well as creating a CPT (Coaches) and saving some fields to the CPT's custom fields. I'm using generic fields to save these custom fields to the CPT using this code in my functions.php file:

function save_coach_cpt_init($post_id, $form_data) {

	if ($form_data['id']==127)
    {
	    $post_id = wp_insert_post( array(
	                    'post_status' => 'publish',
	                    'post_type' => 'coach',
	                    'post_title' => $_POST['first_name'],
	                    'post_content' => ''
	                ) );
	    update_post_meta($post_id, 'wpcf-coach-first-name', $_POST['first_name']);
		update_post_meta($post_id, 'wpcf-coach-last-name', $_POST['last_name']);
		update_post_meta($post_id, 'wpcf-coach-email', $_POST['user_email']);
		update_post_meta($post_id, 'wpcf-coach-street-address', $_POST['coach-street-address']);
		update_post_meta($post_id, 'wpcf-coach-phone', $_POST['coach-phone']);
		update_post_meta($post_id, 'wpcf-coach-website', $_POST['coach-website']);
		update_post_meta($post_id, 'wpcf-coach-zip-code', $_POST['coach-zip-code']);
		update_post_meta($post_id, 'wpcf-coach-description', $_POST['coach-description']);
		update_post_meta($post_id, 'wpcf-coach-gender', $_POST['coach-gender']);
		update_post_meta($post_id, 'wpcf-coach-video', $_POST['coach-video']);
		update_post_meta($post_id, 'wpcf-coach-specialties', $_POST['coach-specialties']);
		update_post_meta($post_id, 'wpcf-years-practicing', $_POST['years-practicing']);
		update_post_meta($post_id, 'wpcf-coach-photo', $_POST['coach-photo']);
		update_post_meta($post_id, 'wpcf-coaching-certificate-image', $_POST['coaching-certificate-image']);
		update_post_meta($post_id, 'wpcf-coach-certificate', $_POST['coach-certificate']);
		update_post_meta($post_id, 'wpcf-training-qualifications-experience', $_POST['training-qualifications-experience']);
		update_post_meta($post_id, 'wpcf-coach-street-address', $_POST['coach-street-address']);
		update_post_meta($post_id, 'wpcf-coach-state', $_POST['coach-state']);
		update_post_meta($post_id, 'wpcf-coach-city', $_POST['coach-city']);
		update_post_meta($post_id, 'wpcf-coach-linkedin-profile', $_POST['coach-linkedin-profile']);
		update_post_meta($post_id, 'wpcf-coach-education', $_POST['coach-education']);
		update_post_meta($post_id, 'wpcf-coach-fee', $_POST['coach-fee']);
	}
}


All the fields are being passed fine to the CPT except the Checkbox field. This is the generic field for that:

  [cred_generic_field type='checkboxes' field='coach-specialties']
        {
        "default":[],
        "options":[{"value":"111","label":"School counselors"},{"value":"222","label":"Career counselor"},{"value":"333","label":"Rehabilitation counselor"},{"value":"444","label":"Mental health counselor"},{"value":"555","label":"Substance abuse and behavioral disorder counselor"},{"value":"666","label":"Marriage and family therapist"}]
        }
        [/cred_generic_field]
#1198825

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Timothy,

Do these values in the custom field for "coach-specialties" corresponding with the values that you are using in Types when the field is created?

I've checked with our team for further advise on this But i'll continue doing some basic debugging with you .

Thanks,
Shane

#1199120

I believe so, here is a screenshot of the Coach Specialties custom field:

hidden link

Tim

#1199246

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Timothy,

I regret to inform that this will take some amount of custom coding to achieve.

The first thing I suggest is that you open a feature request here https://toolset.com/home/contact-us/suggest-a-new-feature-for-toolset/

This way you can make a suggestion to have an API for saving the checkbox fields programatically.

Now onto the hard part now each checkbox group is stored as an ARRAY. In order to know how they are stored we would need to look into your database because it is stored relatively to your post so the unique identifier is different. We would need to know the identifier of each value Checked and Unchecked in order to make this work.

Is it possible to use a single line field to store the checked value from your form instead of trying to populate this using checkboxes ?

Also another alternative is that you can use taxonomies to do this as well.

Please let me know.

Thanks,
Shane

#1199272

Oh, but I see many posts with people using generic fields to save to cpt custom fields. Has the issue of saving generic checkboxes field to custom checkboxes field never come up before? That seems odd.

"Is it possible to use a single line field to store the checked value from your form instead of trying to populate this using checkboxes?" - yes I guess that would be possible, then I could manually adjust if I had to. Is it possible to save a generic checkboxes field to a single line field?

Taxonomies - I guess I could do this as well. Is this easy to set up?

Tim

#1199322

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Timothy,

I suspect that these posts are saving to a non checkbox , radio or select field. This issue has come up before and we've taken it up with our team again to have some API implemented for this exact scenario.

Can you confirm that you've made the feature request? As this will also help to push our development team for this.

I would suggest that we use taxonomies.

So what you will need to do is go to Types -> Taxonomy and create the custom taxonomy called Coach Speciality and then assign that taxonomy to the CPT.

Next you will go into that custom taxonomy on the post by going to CPT -> Coach Speciality and populate the taxonomy with the options.

Finally this code here.

[cred_generic_field type='checkboxes' field='coach-specialties']
      {
      "default":[],
      "options":[{"value":"111","label":"School counselors"},{"value":"222","label":"Career counselor"},{"value":"333","label":"Rehabilitation counselor"},{"value":"444","label":"Mental health counselor"},{"value":"555","label":"Substance abuse and behavioral disorder counselor"},{"value":"666","label":"Marriage and family therapist"}]
      }
      [/cred_generic_field]


Given that you've named your taxonomy terms exactly as you have it here you will need to change the value of each of these items in options to the ID of the term.

The term ID can be found in the URL when you go to edit each individual term.

Example URL would be.

example.com/wp-admin/term.php?taxonomy=category&tag_ID=35&post_type=post&wp_http_referer=%2Ftoolset_beta%2Fwp-admin%2Fedit-tags.php%3Ftaxonomy%3Dcategory

Notice you see the tag_ID=35, this would mean it is the ID of the term i am editing.

So how this would look in code now would be


wp_set_post_terms( $post_id, $_POST['coach-specialties'], 'taxonomy-slug-goes-here');

Now you will replace the "taxonomy-slug-goes-here" with the slug of the created taxonomy in Types.

Please let me know if this was clear.
Thanks,
Shane

#1199580

Excellent, this works. And I have made the feature request.

Tim

#1199960

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Timothy,

Awesome, happy I was able to assist you.

You can mark the ticket as resolved if everything is well.

Thanks,
Shane

#1200313

My issue is resolved now. Thank you!