Add Wordpress User meta information in admin - wordpress

I'm looking to add a custom field into my users section of the wordpress admin, I have a registration field that has an input for company, i can see the company value in the wp_usermeta table but need to get it in the users column in the admin, any ideas on how to achieve this?

You will need to add action and filter hooks for showing custom value in users table. Place the following into your theme's functions.php. Notice: 'PLACE COLUMN NAME HERE' and 'PLACE META KEY HERE':
function add_custom_column_name($columns) {
$columns['columns_array_name'] = 'PLACE COLUMN NAME HERE:';
return $columns;
}
function show_custom_column_values($value, $column_name, $user_id) {
if ( 'columns_array_name' == $column_name )
return get_user_meta( $user_id, 'PLACE META KEY HERE', true );
return $value;
}
add_filter('manage_users_columns', 'add_custom_column_name');
add_action('manage_users_custom_column', 'show_custom_column_values', 10, 3);

Related

Can you edit WordPress custom post types directly from the archive list page?

Is there a way to edit WordPress custom post types directly from the archive list page. For example, you would use the "list" view to edit the posts and pages in that custom post type, including all o the custom fields.
Option 1: Using a plugin
If you're using ACF, the easiest way to do this would be with ACF Quick Edit Fields plugin.
Option 2: Using code
If you don't want to use a plugin, you need to:
Add columns to the list page
Get the fields and display it in these columns
You can add the following code to your theme's functions.php:
Add columns for the data you want displayed in the list
// Replace 'cpt_name' with your custom post type name
function add_custom_columns ( $columns ) {
$columns['custom_field_1'] = __( 'Custom Field 1 Column Name' );
$columns['custom_field_2'] = __( 'Custom Field 2 Column Name' );
return $columns;
}
add_filter ( 'manage_cpt_name_posts_columns', 'add_custom_columns' );
Get the fields and display it in the columns
function display_cf_columns ( $column, $post_id ) {
switch ( $column ) {
case 'custom_field_1':
echo get_post_meta ( $post_id, 'post_meta_1', true );
break;
case 'custom_field_2':
echo get_post_meta ( $post_id, 'post_meta_1', true );
break;
}
}
add_action ( 'manage_cpt_name_posts_custom_column', 'display_cf_columns', 10, 2 );

How to update Order meta data, from a WooCommerce Action

I'm trying to add some custom meta_data to a WooCommerce Order, by running a Order action.
Here is my code:
function custom_add_order_actions( $actions ){
global $theorder;
$actions['my_custom_action'] = 'My custom action';
return $actions;
}
add_action( 'woocommerce_order_actions', 'custom_add_order_actions' );
function custom_add_single_action( $order ){
// Non of these change anything on the order
$order->set_billing_first_name( 'A new test name' );
$order->update_post_meta( 'a_test_field', 'Test field value' );
update_post_meta( $order->get_id(), 'a_test_field', 'Some other value' );
// $order->save(); // I even tried adding this as well, but it doesn't change anything.
}
add_action( 'woocommerce_order_action_my_custom_action', 'custom_add_single_action' );
How do I change the order (or specifically, post_meta fields for an order) from inside an action?
A example
Imagine that I add a post_meta field, with the field name (key): a_test_field.
It's currently an ACF-field, but it's the same for regular WordPress custom fields.
If I change the value of the field and press 'Update', then the value changes:
So far so good. Now the value of the field is 'Foobar'.
What's wierd is that even if I do this:
add_action( 'woocommerce_order_action_my_custom_action', 'custom_add_single_action' );
function custom_add_single_action( $order ){
update_post_meta( $order->get_id(), 'a_test_field', 'A new value' );
die(); // This die is vital, to make the change in the database.
}
Then I can see the value change in the database to 'A new value'.
But if I just do this:
add_action( 'woocommerce_order_action_my_custom_action', 'custom_add_single_action' );
function custom_add_single_action( $order ){
$order->update_post_meta( 'a_test_field', 'A new value' );
// No die(); here...
}
Then the value remains 'Foobar' in the database.
Sorry but the following lightly revisited code works (Selecting the action and click on the button arrow):
add_action( 'woocommerce_order_actions', 'add_custom_order_action' );
function add_custom_order_action( $actions ){
$actions['my_custom_action'] = __('My custom action', 'WooCommerce');
return $actions;
}
add_action( 'woocommerce_order_action_my_custom_action', 'triggered_custom_order_action' );
function triggered_custom_order_action( $order ){
$order->update_meta_data( '_test_1_custom_field', 'AAFFBB9977' );
$order->save();
update_post_meta( $order->get_id(), '_test_2_custom_field', 'Some other value' );
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
Note: Order actions are mostly used for some other things than what you are trying to do.
Now when using a meta box with an input field (as you are showing), on submit, you should save that field value using the action hook save_post_shop_order like in those related threads:
Metabox with multi checkbox in WooCommerce admin single orders
Dynamic custom order numbers based on payment method
Metabox with multiple custom fields for WooCommerce admin order pages

Update ACF select field data programmatically

I have a ACF select field with a field name of "sales_rep". It is populated with:
John Doe
Bart Simpson
Eric Cartman
The field is set to show on the "User Form" and only show when the "Current User Role" is an admin.
The field shows on each user page and I can manually change it to the sales rep I want and save it.
The issue is that I want to be able to programmatically update it whenever the user profile is saved. I will eventually add more code to determine the sales rep but I simplified the issue for this post.
Below is my code in functions.php:
Attempt 1: No error and no change to value
// Update sales rep
add_action( 'edit_user_profile_update', 'update_rep' );
add_action( 'personal_options_update', 'update_rep' );
function update_rep( $user_id )
{
update_user_meta( $user_id, 'sales_rep', 'Eric Cartman' );
}
Attempt 2: No error and no change to value
// Update sales rep
add_action( 'edit_user_profile_update', 'update_rep' );
add_action( 'personal_options_update', 'update_rep' );
function update_rep( $user_id )
{
$field_key = "sales_rep";
$value = array("Eric Cartman");
update_field( $field_key, $value, $user_id );
}
Seems like user_ prefix could fix the issue.
Try to get/update fields with user_ prefix.
F.e:
update_field( $field_key, $value, "user_$user_id" );
And for update_user_meta,
Calling delete_user_meta before could solve the problem.
User meta could have duplicate keys.
Try this one
delete_user_meta( $user_id, $field_key );
add_user_meta( $user_id, $field_key, $value );
I think #Andrii Kovalenko is right, user_ prefix could be your issue.
Also noticed you are saving acf field data as an array("Eric Cartman").
I am assuming your ACF select field predefined select options look like this...
John Doe
Bart Simpson
Eric Cartman
This should work for your acf option...
// is run when you edit YOUR profile, and save it
add_action('personal_options_update', 'handle_user_profile_update' );
// is run when you edit ANY OTHER profile and save it
add_action('edit_user_profile_update', 'handle_user_profile_update' );
// on user profile update function handler
function handle_user_profile_update($user_id) {
// update sales rep
update_sales_rep_field($user_id);
// add anymore on user update profile functions here...
}
// update sales rep field
function update_sales_rep_field($user_id) {
// our values
$value = 'Eric Cartman';
// update acf user field by user id
update_field('sales_rep', $value, 'user_' . $user_id);
}
Out of curiosity, are these sales reps actually users in your wordpress site?
If so you can dynamically populate the select sales_rep user field with sales rep users on your site like this...
function render_sales_rep_select_field($field) {
// reset sales_rep select field choices
$field['choices'] = [];
// get users with user role sales
$args = array(
'role' => 'sales', // your sales reps user role
'orderby' => 'user_nicename',
'order' => 'ASC'
);
// get user with args from above
$users = get_users( $args );
// for each sales rep user
foreach ( $users as $user ) {
// build our sales rep select field choices by user_id > full name
$field['choices'][$user->id] = $user->first_name . ' ' . $user->last_name
}
// return the select field
return $field;
}
// load user field with our custom select drop of sales rep users
add_filter('acf/load_field/name=sales_rep', 'render_sales_rep_select_field');
With this method you won't need to update the field when the user saves changes to their profile.
For example if you get_field like this...
// get user field sales rep array
get_field('sales_rep', 'user_' . $user_id ); // random user id
it will return an array of user_id and full name...
Array
(
[5] => Eric Cartman
)
In your acf user select field settings you will need select return both value and label.

How to display a user's avatar by user-id in wordpress via shortcode?

I've looked through lots of documents, few of them showed a shortcode to display the avatar using user_id.
The closest one is from Github, and it displays the current logged-in user, like this:
<?php
function shortcode_user_avatar() {
if(is_user_logged_in()) { // check if user is logged in
global $current_user; // get current user's information
get_currentuserinfo();
return get_avatar( $current_user -> ID, 24 ); // display the logged-in user's avatar
}
else {
// if not logged in, show default avatar. change URL to show your own default avatar
return get_avatar( 'http://1.gravatar.com/avatar/ad524503a11cd5ca435acc9bb6523536?s=64', 24 );
}
}
add_shortcode('display-user-avatar','shortcode_user_avatar');
?>
But this isn't enough, what I want is to add a parameter for me to choose the userid, and it will end like this:
[display-user-avatar id="user-id"]
Can anybody show me the way to make it?
Thanks!
I've already solve this problem, here is the code:
function shortcode_user_avatar($atts, $content = null) {
extract( shortcode_atts(
array('id' => '0',), $atts
)
);
return get_avatar( $user_id, 96 ); // display the specific user_id's avatar
}
add_shortcode('avatar','shortcode_user_avatar');
Just paste it to theme's functions.php and enter the shortcode [avatar id="xxx"], and replace "xxx" to the user id.
It's actually my first shortcode, and I'm really happy that it's working!

Extra Profile Field Not Showing In Wordpress User Profile

I have created a an extra profile field on Buddypress registration page for users to select their native language using the profile fields area on Buddypress.
Once the user has registered and made this selection I want it their input to show on user-edit.php in wordpress backend so the admin of the site can see what native language that user is.
My extra profile field id is 136
How would I get this to show on the user-edit.php page?
Put this function in your theme/functions.php or in plugins/bp-custom.php
It should appear at the bottom of user-edit.php
function show_136_field ( $user ) {
$field_value = xprofile_get_field_data( 136, $user->ID, $multi_format = 'comma' );
echo "<br />Language: " . $field_value . "<br />";
}
add_action( 'edit_user_profile', 'show_136_field' );
You can also get the field value with the field name rather than id.
$field_value = xprofile_get_field_data( 'Language', $user->ID, $multi_format = 'comma' );
If the $field_value is an array and $multi_format = 'comma', a csv string will be returned.

Resources