0

I have a basic Admin panel allowing Admin to save options using Checkbox. Checkboxes are used as multiple selection is necessary for the option

So, Admin Option - Checkbox 1 - Checkbox 2 - Checkbox 3 etc

My Checkboxes are generated on the fly, successfully with

<input type="checkbox" name="firm" id="firm-<?php echo esc_attr( $term->slug ); ?>" value="<?php echo esc_attr( $term->slug ); ?>" <?php checked( true, is_object_in_term( $user->ID, 'firm', $term ) ); ?> /><input type="checkbox" name="firm" id="firm-<?php echo esc_attr( $term->slug ); ?>" value="<?php echo esc_attr( $term->slug ); ?>" <?php checked( true, is_object_in_term( $user->ID, 'firm', $term ) ); ?> />

Then when saving my option, I am trying to do it with

wp_set_object_terms( $user_id, array( $term ), 'firm', false);

BUt only the last term is saved and not the array (if more than 1 checked)

Any idea?

1 Answer 1

1

Change the name of the checkboxes fromfirm to firm[].

Then, when you go to save the checkboxes

$terms = $_POST['firm'];

will give you an array of term slugs that can be checked/sanitized prior to using wp_set_object_terms to add the terms...

wp_set_object_terms( $user_id, $terms, 'firm', false);
2
  • Hi Stephen, Thanks for your answer. You put me in the right direction, I was checking $_post['firm'] with $term = esc_attr( $_POST['firm'] ) which was causing problem. Thanks again
    – salocin
    Feb 6, 2012 at 9:34
  • @Salocin - no problem! Could you except answers that solve your questions - it helps others know it's been solved, and what the solution was :D. Feb 6, 2012 at 9:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.