How do I get the user ID of the user that was updated in WordPress?

You’re looking for profile_update & user_register. user_register is called after a user is created. add_action( ‘user_register’, ‘myplugin_registration_save’, 10, 1 ); function myplugin_registration_save( $user_id ) { // Do stuff with $user_id } profile_update is called after a user is updated. add_action( ‘profile_update’, ‘my_profile_update’, 10, 2 ); function my_profile_update( $user_id, $old_user_data ) { // Do stuff with … Read more

Delete Users without a First and Last Name

Replace the prefix in wp_users and wp_usermeta table names for the one you use. Make backup of data from both tables before you start to delete users. DELETE u FROM wp_users u LEFT JOIN wp_usermeta um1 ON u.id = um1.user_id AND um1.meta_key = ‘first_name’ LEFT JOIN wp_usermeta um2 ON u.id = um2.user_id AND um2.meta_key = … Read more

Multisite – maximum number of users with specific role

Ok here is how I did it: add_action( ‘editable_roles’ , ‘hide_editable_roles’ ); function hide_editable_roles( $roles ){ $blog_id = get_current_blog_id(); // Get current subsite id switch($blog_id) { // Define different max agents numbers depending on subsite id case 6: $max_agents = 10; //for subsite id#6 we can have maximum 10 agents break; case 7: //for subsite … Read more

create new users in db starting at what ID?

You don’t give them IDs. The user ID column in the database is an ‘auto increment’ column, meaning that the value is automatically created whenever a row is added. So just add your users without an ID and the database will create one for you.

How do I have now a duplicated user entry if this is not allowed (and I cannot replicate it)?

Yes, WordPress checks for duplicate emails internally, but not duplicate usernames To test this I ran this several times via wp shell: wp_create_user( ‘test’, ‘password’, ‘[email protected]’ ); The result on the second attempt was: => class WP_Error#1962 (2) { public $errors => array(1) { ‘existing_user_email’ => array(1) { [0] => string(42) “Sorry, that email address … Read more

How to prevent a specific users’s profile photo (gravatar) from showing on the frontend to other users?

Use this hook function to replace user’s avatar (which is passed in as HTML <img …> tag) if user’s ID or email matches your condition. You can use any other default image, or get individual replacement image for each user. function my_get_avatar($avatar, $id_or_email, $size, $default, $alt) { if ($id_or_email == “[email protected]”) { $img = “http://path.to/default/avatar.jpg”; … Read more

Get current user data

If you just want to write a single <option> for the current user, you can get the user object from wp_get_current_user() – you don’t need a WP_User_Query at all: function getСurrentUserForFilter() { if ( is_user_logged_in() ) { $user = wp_get_current_user(); echo ‘<option value=”‘.$user->ID.'”>’.esc_html($user->user_email).'</option>’; } }

is_user_logged_in() throwing undefined function error

It’s failing because WordPress isn’t loaded. Instead of making AJAX requests directly to PHP files in your theme, make them to a REST API endpoint. For example: add_action( ‘rest_api_init’, function () { register_rest_route( ‘petebolduc/v1’, ‘/test/’, array( ‘callback’ => ‘petebolduc_ajax’ ) ); } ); function petebolduc( $parameters ) { $foo = $parameters[‘foo’]; return “foo is ” … Read more