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 liked to customize my profile page by adding new fields to get more info from the user like "Gender" or "Speaking Languages". I managed to get the text input form to work, the problem I having now is on the radio and checkbox type of input. Here is my code:

function my_user_field( $user ) {
            ?>
                <h3><?php _e('More About You'); ?></h3>
                <table class="form-table">
                    <tr>
                        <th>
                            <label for="Dealing Type"><?php _e('Gender'); ?>
                        </label></th>
                        <td><span class="description"><?php _e('Gender?'); ?></span><br>
                        <label><input type="radio" name="dealing" value="Male">Male<br /></label>
                        <label><input type="radio" name="dealing" value="Female">Female<br /></label>

                        </td>
                    </tr>

                    <tr>
                        <th>
                            <label for="company"><?php _e('Company'); ?>
                        </label></th>
                        <td>
                          <span class="description"><?php _e('Insert Your Company name'); ?></span><br>
                            <input type="text" name="company" id="company" value="<?php echo esc_attr( get_the_author_meta( 'company', $user->ID ) ); ?>" class="regular-text" /><br />

                        </td>
                    </tr>

                    <tr>
                        <th>
                            <label for="language"><?php _e('Language'); ?>
                        </label></th>
                        <td><input type="checkbox" name="Engilsh" value="Malay" /> English<br />
                            <input type="checkbox" name="language" value="Mandarin" /> Mandarin<br />


                        </td>
                    </tr>
                </table>

            <?php }


            function my_save_custom_user_profile_fields( $user_id ) {
                if ( !current_user_can( 'edit_user', $user_id ) )
                    return FALSE;
                update_usermeta( $user_id, 'gender', $_POST['gender'] );
                update_usermeta( $user_id, 'company', $_POST['company'] );
                update_usermeta( $user_id, 'language', $_POST['language'] );

            }
            add_action( 'show_user_profile', 'my_user_field' );
            add_action( 'edit_user_profile', 'my_user_field' );
            add_action( 'personal_options_update', 'my_save_custom_user_profile_fields' );
            add_action( 'edit_user_profile_update', 'my_save_custom_user_profile_fields' );

The "Gender" and "Languages" are not passing through the $_post[]. I am new to coding, hope you guys can help.

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

You are missing the "checked" value for the inputs

<input type="checkbox" name="language" <?php if (get_the_author_meta( 'language', $user->ID) == 'Mandarin' ) { ?>checked="checked"<?php }?> value="Mandarin" /> Mandarin<br />

Also, the usermeta is dealing but your are checking for $_POST['gender']

Finally, you should have one usermeta for English and other for Mandarin, as they are not mutually exlcusive

[edit: working code] [edit2: multiple languages]

<?php
$lingo = array('en' => 'English', 'md' => '普通話', 'es' => 'Español', 'fr' => 'Français', 'pt' => 'Português');

function my_user_field( $user ) {
    $gender = get_the_author_meta( 'dealing', $user->ID);
    $company = esc_attr( get_the_author_meta( 'company', $user->ID ) );
?>
    <h3><?php _e('More About You'); ?></h3>
    <table class="form-table">
        <tr>
            <th>
                <label for="Dealing Type"><?php _e('Gender'); ?>
            </label></th>
            <td><span class="description"><?php _e('Gender?'); ?></span><br>
            <label><input type="radio" name="dealing" <?php if ($gender == 'Male' ) { ?>checked="checked"<?php }?> value="Male">Male<br /></label>
            <label><input type="radio" name="dealing" <?php if ($gender == 'Female' ) { ?>checked="checked"<?php }?> value="Female">Female<br /></label>

            </td>
        </tr>
        <tr>
            <th>
                <label for="company"><?php _e('Company'); ?>
            </label></th>
            <td>
              <span class="description"><?php _e('Insert Your Company name'); ?></span><br>
                <input type="text" name="company" id="company" value="<?php echo $company; ?>" class="regular-text" /><br />
            </td>
        </tr>
        <tr>
            <th>
                <?php _e('Language'); ?>
            </th>
            <td><?php
                global $lingo;
                foreach($lingo as $key => $value) {
                    $code = 'language_'.$key;
                    $lang = get_the_author_meta( $code, $user->ID);
                     ?>
                    <label><input type="checkbox" name="<?php echo $code; ?>" <?php if ($lang == 'yes' ) { ?>checked="checked"<?php }?> value="yes" /> <?php echo $value; ?></label><br />
                <?php }
            ?>
            </td>
        </tr>
    </table>
<?php 
}


function my_save_custom_user_profile_fields( $user_id ) {
    if ( !current_user_can( 'edit_user', $user_id ) )
        return FALSE;

    update_usermeta( $user_id, 'dealing', $_POST['dealing'] );
    update_usermeta( $user_id, 'company', $_POST['company'] );

    global $lingo;
    foreach($lingo as $key => $value) {
        $code = "language_".$key;
        update_usermeta( $user_id, $code, $_POST[$code] );
    }
}

add_action( 'show_user_profile', 'my_user_field' );
add_action( 'edit_user_profile', 'my_user_field' );
add_action( 'personal_options_update', 'my_save_custom_user_profile_fields' );
add_action( 'edit_user_profile_update', 'my_save_custom_user_profile_fields' );
share|improve this answer
    
Ok, thanks for your reply. I can see how it works, however as for the checkbox type, is that possible to use one usermeta for languages to store both English and Mandarin? –  dev-jim Apr 21 '12 at 12:23
    
I'm not sure, but I don't think so... Anyway, why would you want that? –  brasofilo Apr 21 '12 at 13:10
    
Because I might have plenty of languages to be checked, for the simplification sake, it is better to make it simple in coding. I wondering using array would be possible? –  dev-jim Apr 21 '12 at 15:15
    
Updated the code to support any number of languages –  brasofilo Apr 21 '12 at 15:59
    
Thanks brasofilo, you are rock!! But how about display the checked item in the front page? –  dev-jim Apr 21 '12 at 16:14
show 6 more comments

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.