Add Custom User Meta to the Users Admin page in WordPress

Report
Question

Please briefly explain why you feel this question should be reported .

Report
Cancel
Here is how you can add custom user meta to the users admin page in wordpress.
<?php

// Hooks near the bottom of profile page (if current user) 
add_action('show_user_profile', 'custom_user_profile_fields');

// Hooks near the bottom of the profile page (if not current user) 
add_action('edit_user_profile', 'custom_user_profile_fields');

// @param WP_User $user
function custom_user_profile_fields( $user ) {
?>
    <table class="form-table">
        <tr>
            <th>
                <label for="code"><?php _e( 'Custom Meta' ); ?></label>
            </th>
            <td>
                <input type="text" name="code" id="code" value="<?php echo esc_attr( get_the_author_meta( 'code', $user->ID ) ); ?>" class="regular-text" />
            </td>
        </tr>
    </table>
<?php
}


// Hook is used to save custom fields that have been added to the WordPress profile page (if current user) 
add_action( 'personal_options_update', 'update_extra_profile_fields' );

// Hook is used to save custom fields that have been added to the WordPress profile page (if not current user) 
add_action( 'edit_user_profile_update', 'update_extra_profile_fields' );

function update_extra_profile_fields( $user_id ) {
    if ( current_user_can( 'edit_user', $user_id ) )
        update_user_meta( $user_id, 'code', $_POST['code'] );
}

?>

 

0
WordPress 1 month 0 Answer 19 views 0

About Irfan

I love to Develop Websites, Applications and UI Design. I'm a Certified Developer, blogger and owner of Codexjunction.com. My friends call me WordPress Expert too. I like to learn and share technical stuff. For more info quick message me, I'll help you out.

Leave an answer

Captcha Click on image to update the captcha .

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>