Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add Custom User-Meta to the Users Admin page in WordPress

Tags:

php

wordpress

I have created a special form within my site that allows my users to enter a key. I am using add_user_meta() to add the meta data into the database. I want to be able to see this key when I click on users in the admin center.

How would I go about adding to this column?

Below is the meta data info im using

add_user_meta($userId,'code','12345');

We just want to be able to add it to the view on users.php in the table displaying username email and role.

I have used code like this to display the user id but I can not figure out how to display their meta.

add_filter('manage_users_columns', 'pippin_add_user_id_column');
function pippin_add_user_id_column($columns) {
    $columns['user_id'] = 'User ID';
    return $columns;
}

add_action('manage_users_custom_column',  'pippin_show_user_id_column_content', 10, 3);
function pippin_show_user_id_column_content($value, $column_name, $user_id) {
    $user = get_userdata( $user_id );
    if ( 'user_id' == $column_name )
        return $user_id;
    return $value;
}
like image 391
James Avatar asked May 27 '15 20:05

James


People also ask

What is WordPress User Meta and how to add user meta?

Here’s the simplest definition: User meta is “custom fields for your users.” In other words, just as WordPress post meta —also called custom fields—lets you add any information you want to about your posts, WordPress user meta lets you add any information you want to about your users. Now: Why would you want to add user meta?

What is the get_user_meta() function in WordPress?

The get_user_meta () function is quite simple and quite important: It’s how you retrieve a stored piece of user meta so you can do something with it. It looks as follows: The ID of the user whose user meta you want to retrieve. A user meta key. In the example above, which we’ll continue here, the key we specified is wpshout_found_this_post.

How do I create a Meta Profile form in WordPress?

Go to User Meta >> Forms. Create a form, give a name to your form and populate it with fields. Write shortcode to your page or post. e.g.: Shortcode: [user-meta-profile form=’your_form_name’]

What is “user meta – user profile builder and user management plugin”?

“User Meta – User Profile Builder and User management plugin” is open source software. The following people have contributed to this plugin. Translate “User Meta – User Profile Builder and User management plugin” into your language.


3 Answers

This example was created with the help of these two pages from the WordPress codex.

https://codex.wordpress.org/Plugin_API/Action_Reference/edit_user_profile https://codex.wordpress.org/Plugin_API/Action_Reference/personal_options_update

It is for displaying and updating the custom user meta data.

<?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'] );
}

?>
like image 77
Danijel Avatar answered Nov 15 '22 02:11

Danijel


The answer above from Mordred worked for me after changing the second add_filter to add_action. Here's the modified code:

function yoursite_manage_users_columns( $columns ) {

    // $columns is a key/value array of column slugs and names
    $columns[ 'custom_field' ] = 'Subscription';

    return $columns;
}

add_filter( 'manage_users_columns', 'yoursite_manage_users_columns', 10, 1 );

function yoursite_manage_users_custom_column( $output, $column_key, $user_id ) {

    switch ( $column_key ) {
        case 'custom_field':
            $value = get_user_meta( $user_id, 'custom_field', true );

            return $value;
            break;
        default: break;
    }

    // if no column slug found, return default output value
    return $output;
}

add_action( 'manage_users_custom_column', 'yoursite_manage_users_custom_column', 10, 3 );
like image 33
Chris Bartholomew Avatar answered Nov 15 '22 02:11

Chris Bartholomew


To add custom user_meta fields to users.php you need to do the following:

function yoursite_manage_users_columns( $columns ) {

    // $columns is a key/value array of column slugs and names
    $columns[ 'custom_field' ] = 'Subscription';

    return $columns;
}

add_filter( 'manage_users_columns', 'yoursite_manage_users_columns', 10, 1 );

function yoursite_manage_users_custom_column( $output, $column_key, $user_id ) {

    switch ( $column_key ) {
        case 'custom_field':
            $value = get_user_meta( $user_id, 'custom_field', true );

            return $value;
            break;
        default: break;
    }

    // if no column slug found, return default output value
    return $output;
}

add_filter( 'manage_users_custom_column', 'yoursite_manage_users_custom_column', 10, 3 );
like image 23
Mordred Avatar answered Nov 15 '22 04:11

Mordred