Randomize Users

I don’t see an “orderby Rand()” parameter for either get_users or WP_User_Query. There is a filter called pre_user_query that could be used but I am not sure I see the benefit of that when shuffle will randomize the array you already have. $args = array( ‘fields’ => ‘all_with_meta’, ‘exclude’ => array(1), ); $users = get_users( … Read more

The Simple and Correct Way to Add User Meta

Trying to detect when a UI was triggered, is indeed the wrong general approach (unless you want to tie to the UI). A proper one will be to hook on the user_register action. add_action( ‘user_register’, ‘wpse223196_registration_save’, 10, 1 ); function wpse223196_registration_save( $user_id ) { add_user_meta($user_id, ‘score’, 5); }

How to access author data from header action

The global $authordata variable is only available by default when $wp_query->is_author() && isset($wp_query->post) condition is satisfied. It means that you can’t access $authordata inside a single post page. You may try to get author data via $wp_query: add_action(‘wp_head’, function() { global $wp_query; $userdata = get_userdata($wp_query->post->post_author); $avatar_url = get_avatar_ur($userdata->user_email); … }, 10, 0);

Display author name, outside the loop, if they haven’t published a custom post

author’s name or logged in user’s name? can use global $current_user; or wp_get_current_user(); if the user is logged in. if( is_user_logged_in() ) { $current_user = wp_get_current_user(); echo $current_user->user_firstname; } for specific user role you can check $current_user->roles array. Reference: https://codex.wordpress.org/Function_Reference/wp_get_current_user https://codex.wordpress.org/Function_Reference/get_currentuserinfo

a:0:{} is replaced into database as s:6:”a:0:{}”;

API calls are API calls, not database writes. What and how information is stored in the DB is usually best left as an unknown since, unless explicitly defined in the API, it might change. Specifically in this case, wordpress will serialize the value being passed, and since you are passing a string it is serialized … Read more

Problem when try to add ++1 for user meta

The problem here is twofold: the ++ operator is applied on an array. the user id and the meta key need to be swapped in get_user_meta() So use get_user_meta( $user_id, “_user_count”, true ); to get user meta. Note that the third input parameter is false by default and an array is returned. Setting it to … Read more

Check role of Username then echo something if administrator [closed]

@terminator answer might work but there’s no need to create an array and use array_intersect I think since you’re only looking for one role. This might be more comprehensible: $current_user = wp_get_current_user(); $roles = (array) $current_user->roles; if (in_array(‘administrator’, $roles) { /* echo whatever */ } Don’t forget to check that ‘administrator’ is acutally the role … Read more