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 found this great article (right here) about creating and saving custom fields for user profiles. The problem is that it only saves text fields. I can't save checkboxes, radio, or any other field types.

Here is the code I am using in the functions.php of my theme:

<?php

add_action( 'show_user_profile', 'my_show_extra_profile_fields' );
add_action( 'edit_user_profile', 'my_show_extra_profile_fields' );

function my_show_extra_profile_fields( $user ) { ?>


    <h3>Extra profile information</h3>

    <table class="form-table">

        <tr>

            <td>
                <input type="text" name="test" id="test" value="<?php echo esc_attr( get_the_author_meta( 'test', $user->ID ) ); ?>" class="regular-text" /><br />
            </td>
        </tr>

        <tr>

            <td>
                <input type="checkbox" name="test2" id="test2" value="<?php echo esc_attr( get_the_author_meta( 'test2', $user->ID ) ); ?>" class="regular-text" /><br />
            </td>
        </tr>

    </table>


<?php } 

add_action( 'personal_options_update', 'my_save_extra_profile_fields' );
add_action( 'edit_user_profile_update', 'my_save_extra_profile_fields' );

function my_save_extra_profile_fields( $user_id ) {

    if ( !current_user_can( 'edit_user', $user_id ) )
        return false;

    /* Copy and paste this line for additional fields. Make sure to change 'twitter' to the field ID. */
    update_usermeta( $user_id, 'test', $_POST['test'] );
    update_usermeta( $user_id, 'test2', $_POST['test2'] );
}

?>

In this sample, the "test" field saves, but the "test2" field doesn't.

Does anyone know how to save different field types (checkboxes, radio, dropdown, etc.) using this code?

Thanks a lot!

share|improve this question

1 Answer 1

up vote 1 down vote accepted

How do you know that the checkbox value is not being saved? I'm guessing that it is saved to the database; however, in your code, you do not have any method of setting the "checked" attribute for the checkbox to "checked". As such, you will not see the boxed checked.

You'll need something like the following to achieve this effect.

<?php $checked = ( get_the_author_meta( 'test2', $user->ID ) == 'some-value' ) ? ' checked="checked" : ''; ?>
<input type="checkbox" name="test2" id="test2" value="some-value" class="regular-text"<?php echo $checked; ?> /><br />

Just make sure to change "some-value" to the value that you want.

share|improve this answer
    
Hi tollmanz, the way I know its not being saved is because I checked the usermeta table of the database and searched for the newly created field and a value for it. I suspect that the usermeta field wont show in the database if there isn't a value saved for it. So thats why I think the value to that field are not being saved. And another thing is how would your solution work for other field types, such as radios, drop down, etc? Thanks a lot. –  user1893 Oct 3 '11 at 14:46

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.