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;
}
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?
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.
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’]
“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.
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'] );
}
?>
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 );
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 );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With