Add extra field to users

I have this code that adds the check box to the ‘Edit User’ page but when i check it, and refresh the page, the box becomes unchecked… what is wrong?

add_action( 'show_user_profile', 'module_user_profile_fields' );
add_action( 'edit_user_profile', 'module_user_profile_fields' );
function module_user_profile_fields( $user ) 
{ ?>
    <h3>Module Options</h3>

    <table class="form-table">
        <tr>
            <th><label for="module_activation">Module Activation</label></th>
            <td>
                <input id="module_activation" name="module_activation" type="checkbox" value="<?php echo esc_attr( get_the_author_meta( 'module_activation', $user->ID )); ?>" <?php if ( get_the_author_meta( 'module_activation', $user->ID ) == 1  ) echo ' checked="checked"'; ?> />
                <span class="description"><?php _e("Please enter your address."); ?></span>
            </td>
        </tr>
    </table>
<?php }

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

function save_module_user_profile_fields( $user_id ) 
{
    if ( !current_user_can( 'edit_user', $user_id ) ) { return false; }

    update_usermeta( $user_id, 'module_activation', $_POST['module_activation'] );
}

EDIT

Read More

Here is what i got to work.

add_action( 'show_user_profile', 'module_user_profile_fields' );
add_action( 'edit_user_profile', 'module_user_profile_fields' );
function module_user_profile_fields( $user ) 
{ ?>
    <h3>Module Options</h3>

    <table class="form-table">
        <tr>
            <th><label for="module_activation">Module Activation</label></th>
            <td>
                <input id="module_activation" name="module_activation" type="checkbox" value="1" <?php if ( get_the_author_meta( 'module_activation', $user->ID ) == 1  ) echo ' checked="checked"'; ?> />
                <span class="description"><?php _e("Please enter your address."); ?></span>
            </td>
        </tr>
    </table>
<?php }

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

function save_module_user_profile_fields( $user_id ) 
{
    if ( !current_user_can( 'edit_user', $user_id ) ) { return false; }

    update_usermeta( $user_id, 'module_activation', $_POST['module_activation'] );
}

Related posts

Leave a Reply

1 comment

  1. your function never defines a value for that field so when you check if its equal to 1 you never get true.

    try this:

    add_action( 'show_user_profile', 'module_user_profile_fields' );
    add_action( 'edit_user_profile', 'module_user_profile_fields' );
    function module_user_profile_fields( $user ) 
    { ?>
        <h3>Module Options</h3>
    
        <table class="form-table">
            <tr>
                <th><label for="module_activation">Module Activation</label></th>
                <td>
                    <input id="module_activation" name="module_activation" type="checkbox" value="1" <?php if ( get_the_author_meta( 'module_activation', $user->ID ) == 1  ) echo ' checked="checked"'; ?> />
                    <span class="description"><?php _e("Please enter your address."); ?></span>
                </td>
            </tr>
        </table>
    <?php }
    
    add_action( 'personal_options_update', 'save_module_user_profile_fields' );
    add_action( 'edit_user_profile_update', 'save_module_user_profile_fields' );
    
    function save_module_user_profile_fields( $user_id ) 
    {
        if ( !current_user_can( 'edit_user', $user_id ) ) { return false; }else{
            if(isset($_POST['module_activation']) && $_POST['module_activation'] > 0){
                update_usermeta( $user_id, 'module_activation', $_POST['module_activation'] );
            }else{
                delete_usermeta($user_id, 'module_activation');
            }
        }
    }