How to update user role without logout

I think you are on the right track, wp_cache_delete was what finally helped me get an auto-signup with auto-login plugin working… I have this from there: wp_cache_delete($current_user->ID, ‘users’); wp_cache_delete($current_user->user_login, ‘userlogins’); Then see what roles you get after that with: $current_user = wp_get_current_user();

edit profile validation refreshes all field if missing wordpress

Approach 1, disable save button What you can do, is to prevent the user from saving the form if the required fields are not filled. This can be simply done via javascript. function require_fields_script(){ echo ” <script type=”text/javascript”> (function($){ $(‘#submit’).on(‘click’,function(e){ if (!$(‘#email’).val() || !$(‘#nickname’).val()){ e.preventDefault(); if (!$(‘#email’).val()) { window.alert(‘Please enter your email before saving.’); } … Read more

Get users with atleast one post

global $wpdb; $min_posts = 5; // Make sure it’s int, it’s not escaped in the query $author_ids = $wpdb->get_col(“SELECT `post_author` FROM (SELECT `post_author`, COUNT(*) AS `count` FROM {$wpdb->posts} WHERE `post_status`=’publish’ GROUP BY `post_author`) AS `stats` WHERE `count` >= {$min_posts} ORDER BY `count` DESC;”); // Do what you want to $author_ids from here on… This will … Read more

Force display name as full name

This is set during user registration. You can change the value per filter ‘user_register’. Sample code, not tested: /* Plugin Name: First name plus last name as default display name. */ add_action( ‘user_register’, ‘wpse_67444_first_last_display_name’ ); function wpse_67444_first_last_display_name( $user_id ) { $data = get_userdata( $user_id ); // check if these data are available in your real … Read more

How can I get users email (and additional data) from the rest API?

To add the user’s email address in the REST API response, register an additional field to the user object and add the email address: register_rest_field( ‘user’, ‘user_email’, [ ‘get_callback’ => static function (array $user): string { return get_userdata($user[‘id’])->user_email; }, ] ); Please note, however, that this is strongly discouraged because anyone can then see the … Read more

Limiting the number of users

As you can see in the WordPress Option Reference, there’s an option called users_can_register. You can – per default – set it in your (network-)sites settings. 1 => Yes 0 => No As usual: There’s a filter to intercept that from a plugin. “option_{$option_name}” So you can simply attach a callback to this filter and … Read more

Best way to send users password?

You shouldn’t have to send them passwords. That’s a bad idea. Instead, make sure your web server has email setup properly and your user accounts have the correct email addresses. Then all you’ll have to do is send them the link to the Forgot Password link provided by WordPress by default. It looks like this: … Read more

Check if specific username is logged in

wp_get_current_user then compare the object that’s returned user_login proprety with the specific username you’d like to check against. <?php add_action(‘admin_init’, ‘wpse74389_check_username’); function wpse74389_check_username() { $user = wp_get_current_user(); if($user && isset($user->user_login) && ‘username_to_check’ == $user->user_login) { // do stuff } }

Display edit link if post author is current user

If you just have to modify the author.php page, this piece of code will probably work : <?php if( is_user_logged_in() && is_author(get_current_user_id()) ) { edit_post_link(‘edit’, ”, ”); } ?> The first part of the conditions checks if there is a user logged. The second one will be true if the current page is the author … Read more

Is there a way to set a user profile to Draft?

The database table for users holds the user_status as integer: $users_single_table = “CREATE TABLE $wpdb->users ( ID bigint(20) unsigned NOT NULL auto_increment, user_login varchar(60) NOT NULL default ”, user_pass varchar(255) NOT NULL default ”, user_nicename varchar(50) NOT NULL default ”, user_email varchar(100) NOT NULL default ”, user_url varchar(100) NOT NULL default ”, user_registered datetime NOT … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)