Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 693 Vote(s) - 3.59 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Add Custom User-Meta to the Users Admin page in WordPress

#1
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;
}
Reply

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

[To see links please register here]

[To see links please register here]


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

?>


Reply

#3
Realising this is a bit of an old thread, however I was stuck on a very similar problem, and thought I would share what I found which proved to be a very simple solution.

<?php
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;
}
?>

Credit:

[To see links please register here]

Reply

#4
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 );
Reply

#5
The answer above from [Mordred](

[To see links please register here]

) 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 );
```
Reply

#6
To make the additional woocommerce fields editable you can use the following filter (in the example a custom field is added in the billing section).

add_filter('woocommerce_customer_meta_fields', 'add_woocommerce_customer_meta_fields');
function add_woocommerce_customer_meta_fields($fields)
{
if (isset($fields['billing']['fields'])) {
$fields['billing']['fields']['your_custom_meta'] = array(
'label' => __('Friendly name', 'woocommerce'),
'description' => ''
);
}
return $fields;
}
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through