Add custom user meta data

Adding custom field to all existing users. You will have to get the list of users and then, in a loop, set a custom field for each of them. $users = get_users( [ ‘fields’ => ‘id’, ‘meta_key’ => ‘YOUR_META_KEY’, ‘meta_compare’ => ‘NOT EXISTS’ ] ); if ( is_array($users) && count($users) ) { $meta_value = false; … Read more

Update a user profile via frontend

I’ve written this in a hurry and it’s untested and probably sloppy code, but it gives you an idea of how to accomplish this with AJAX. <form id=”update_user”> <div class=”form-group”> <label for=”firstname”>Name</label> <input type=”text” name=”firstname” id=”firstname” value=”<?= esc_attr(wp_get_current_user()->user_firstname) ?>” placeholder=”Vorname”> </div> <div class=”form-group”> <label for=”name”>Name</label> <input type=”text” name=”name” id=”name” value=”<?= esc_attr(wp_get_current_user()->user_lastname) ?>” placeholder=”Name”> </div> <div … Read more

User roles not displaying

You can’t see any roles printed, because the ->roles field is an Array, so you can’t print it using echo. User print_r instead. You also have an error in this line: $user = get_userdata(userID); There is no such thing like userID – it should be $userID.

How to get user ID’s from multiple usernames?

Your call to get_user_by() has a small hiccup in it. It needs to be login rather than user_login // Search these Usernames $usernames = array(‘user1’, ‘user2’); // Fetch the User IDs $prof_ids = array(); foreach ($usernames as $prof_id) { $user = get_user_by(‘login’, $prof_id); $prof_ids[] = $user->ID; } // WP_User_Query arguments $args = array( ‘include’ => … Read more

Get user by meta key – WP multi site

This is the problem: $user = reset( get_users( reset expects a reference to an array, and there’s no mechanism for error checking. So even if it finds the user, you’re misusing the return value. It might be possible to adjust this to use references properly, but references in PHP are something best avoided, and there … Read more

Order users by user role

@CynthiaLara I suppose that you are using a container of WP_USER_QUERY::__construct in the form or WP_USER_QUERY or get_users or something to that effect. You can use ‘meta_key’ => ‘<YOUR_DESIGNATION_META_KEY>’,’orderby’ => ‘meta_value_num, to get results sorted by your meta key’s value. If you have used a texted based value in for this meta_key, please consider using … Read more

Fix ‘Add Role’ Option not there in wordpress 5.2.2

“Add Role” isn’t an option in WordPress, and never has been. If you want to add a role you’ll need a plugin like User Role Editor or Members. If you want to add a role with code, you can do this with the add_role() function: add_role( ‘member’, ‘Member’, [ ‘edit_posts’ => true, ] ); Just … Read more

Custom Field Repeating When Using foreach

if you want to use multiple users, if you will add usernames comma separated in custom field. try below code. function user_avatar() { $user = “”; $names = get_post_meta( get_the_ID(), ‘user_name’, true ); $user_names = array_map(‘trim’, explode(‘,’, $names)); if(!empty($user_names)) { foreach ($user_names as $key => $user_name) { $user = get_user_by( ‘login’, $user_name ); if($user) { … Read more