Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I added new options in user profile page. I added checkbox but it is not saving if all multiple options are checked.

<input type="checkbox" name="neww" class="neww" value="one"<?php  checked(get_the_author_meta( 'neww', $user->ID ),'one' ); ?> />
<span class="description">option one</span><br/>
<input type="checkbox" name="neww" class="neww" value="two"<?php  checked(get_the_author_meta( 'neww', $user->ID ),'two' ); ?> />
<span class="description">option two</span><br/>
<input type="checkbox" name="neww" class="neww" value="three"<?php  checked(get_the_author_meta( 'neww', $user->ID ),'three' ); ?> />
<span class="description">option three</span><br/>

When i var_dump to see what is saved it is saving only the last checked item. Why is this happening? While saving with register settings options i haven't experianced this. can anyone let me know the difference?

Thanks

share|improve this question

1 Answer 1

When you pass three separate values for neww, the value ends up being the last one. So neww would need to be an array to collect all three: neww[]

<input type="checkbox" name="neww[]" class="neww" value="one" />
<input type="checkbox" name="neww[]" class="neww" value="two" />
<input type="checkbox" name="neww[]" class="neww" value="three" />

But since get_the_author_meta returns values of user meta data as a string, the data there would not be able to be stored and returned as an array. So if these are stored as separate values (i.e. you need all three possibilities), you would need to name each checkbox a different name:

<input type="checkbox" name="neww1" class="neww" value="one" />
<input type="checkbox" name="neww2" class="neww" value="two" />
<input type="checkbox" name="neww3" class="neww" value="three" />

When using checkboxes, the user can select none, one, or multiple. So if you are storing a meta value here as a string and only collecting one possible answer, then you need a radio group where only one selection can be chosen. Then you would be able to do it like this:

<input type="radio" name="neww" value="one" <?php checked( $neww, 'one' ); ?> /> One<br />
<input type="radio" name="neww" value="two" <?php checked( $neww, 'two' ); ?> /> Two<br />
<input type="radio" name="neww" value="three"<?php checked( $neww, 'three' ); ?> /> Three
share|improve this answer
    
This way when the page is loaded for first time then there will be a warning of parameter needs to be array in_array because there won't be any value for first time. Why not let be a string value instead of array. When we save using options table, there is no things like this. Do you have anything to say about this? –  rram Mar 15 at 21:20
    
Well... in looking at get_the_author_meta, it looks like you can't store an array for any of the values anyway (and it returns as a string). So if this is dealing specifically with author meta data, then you should probably use separate values for each checkbox rather than a single name. Or possibly you should consider using a radio group if only one value would be accepted. I will amend my anwer. –  butlerblog Mar 15 at 23:28
    
Hi sorry i don't want single option. Remove your radio option answer. I'm okay with passing in array for checkbox –  rram Mar 16 at 9:29

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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