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 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

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'] );
}
share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

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');
        }
    }
}
share|improve this answer
    
Hmm, its closer, but not quite. Its now stuck on 'checked'. –  cnotethegr8 Jul 13 '11 at 19:16
    
@cnotethegr8: sorry about that, i updated the code. –  Bainternet Jul 13 '11 at 19:46
    
how can i reorder the profile fields. Like the custom field to be the first on –  alex May 16 '13 at 12:49
add comment

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.