How to delete specific users in bulk in WordPress? - wordpress

How can WordPress delete users in bulk when the user_id is already known?
Is there an easy way to do this? For example SQL CLI or PHP CLI or something like that. Has anyone already done this?

Use the WordPress CLI. There's a user delete command which can accept a list.
Examples from the docs:
# Delete user 123 and reassign posts to user 567
$ wp user delete 123 --reassign=567
Success: Removed user 123 from http://example.com
# Delete all contributors and reassign their posts to user 2
$ wp user delete $(wp user list --role=contributor --field=ID) --reassign=2
Documentation: https://developer.wordpress.org/cli/commands/user/delete/

Try the following snippet to delete specific users whose ids are known.
function delete_specific_users() {
//Include the user file with the user administration API
require_once( ABSPATH . 'wp-admin/includes/user.php' );
//Get a list of users that belongs to the specified role
$users = get_users( array( 'ID' => array( comma separated list of user_ids you want to delete ) ) );
//Delete all the user of the specified role
foreach ( $users as $user ) {
wp_delete_user( $user->ID );
}
}
// call the method like this.
delete_specific_users();

Related

How to rename the users column in WordPress database

I have this specific requirement using shared database which is shared by laravel. In the db table, all the registered username records gets stored in 'username' column (under users table), so I need to retrieve the username to login in another app that is built in WordPress. By default WordPress retrieve username from 'user_login' column, so what I am looking here is pick username from 'username' column instead of 'user_login'.. This is how I am trying to filter
add_filter( 'manage_users_columns', 'so_25737835' );
function so_25737835( $columns )
{
$columns['user_login'] = 'username';
return $columns;
}
If anyone has something to share then it will be nice.

how should i get all posts for particular user role in wordpress through plug-in

i am using wordpress database as a backend for android application and i have 2 custom roles in the application one is management and second is user.and i am using members plugin for content permission in wordpress which allows to add permissions.now from my plugin i want to get all the posts for users which has management role.these request will be come as a REST based URL and return json data.
try this
create a function to alter the query's where clause:
function authors_where_filter( $where ) {
global $wpdb;
$ids = get_users(array('role' => 'author' ,'fields' => 'ID'));
$where .= " AND post_author IN ($ids)";
return $where;
}
and then before you query just hook it ex:
add_filter('posts_where','authors_where_filter');
$all_posts = new WP_Query(array('posts_per_page' => -1 .....
remove_filter('posts_where');
and you should get all posts of author users in a single query, (well two actually one to get the users and the other is to get the posts)
Source

Wordpress Validate New Nicename/Slug

I have written a custom "Edit Account" script that allows a Wordpress user to update their Wordpress account. Everything is working great, except that I can't seem to find a way to update the user's nicename, which also doubles as the user's URL slug (via the get_author_posts_url function). This is causing issues because when a user changes their name, their slug still contains their original name - not the new one.
I know that the sanitize_title function will generate the new nicename, but I don't know how to verify that it is unique and modify it if it is not before entering it into the DB. I am wondering what built-in functions Wordpress has to handle this. I know I can write my own script to do this, but I would much rather use Wordpress functions. I couldn't find this anywhere in the WP documentation. Thanks!
Here is a function I have written to in lue of a built in function:
function new_user_slug($string){
//GENERATE NEW SLUG
$slug=sanitize_title($string);
//MAKE SURE SLUG IS UNIQUE
$result=mysql_query("SELECT * FROM wp_users WHERE user_nicename='$slug'");
if(mysql_num_rows($result)==0){
return $slug;
}else{
$counter=2;
$kill=0;
while($kill==0){
$mod_slug=$slug."-".$counter;
$result=mysql_query("SELECT * FROM wp_users WHERE user_nicename='$mod_slug'");
if(mysql_num_rows($result)==0){
$kill=1;
}else{
$counter++;
}
}
return $mod_slug;
}
}
This takes a string (the user's updated name) and converts it into the default slug. It then checks the slug against the database to see if it is unique. If it is, the slug is returned. If not, it enters an iteration loop that incrementally changes the slug until it is unique.
Try this:
wp_unique_post_slug( $slug, $post_ID, $post_status, $post_type, $post_parent )
Source: https://codex.wordpress.org/Function_Reference/wp_unique_post_slug
Actually if you use wordpress user functions like wp_insert_user and wp_update_user Wordpress itselt handles duplicate user_nicename entries and adds -n suffix to them.
So your code would be something like this:
function new_nicename( $user_id, $nicename ) {
$nicename = sanitize_title( $nicename );
$user_id = wp_update_user( array( 'ID' => $user_id, 'user_nicename' => $nicename ) );
}

Wordpress - Change forum role from outside bbPress by checking user status

I would like to check the user status and update their bbPress forum role accordingly. (Not the Wordpress role.)
The purpose is to add functionality to the BP-Registration-Options plugin that moderates user registration (In BuddyPress. Currently the plugin sets the user status to 69 while the user is unapproved, and blocks access to BuddyPress functionality. However, the user is still able to login.
When they log-in, bbPress automatically sets the user forum role according to your setting in the back end. In this case it is set to 'spectator'. When the user is approved by the admin their status changes and I want it to also update the bbPress role to 'participant'.
Here is my first attempt:
function bp_registration_options_additional() {
if ( is_user_logged_in() ) {
$user_ID = get_current_user_id();
$user = get_userdata( $user_ID );
if (69 !== $user->user_status ) {
// Here is where I need help.
//How to set the bbPress forum role to 'Participant'?
}
}
}
add_action( 'wp_loaded', 'bp_registration_options_additional' );
Thanks!
WP: 3.8
bbPress: Version 2.5.2
EDIT: A bit of additional information. The meta_key for the forum roles is: wp_capabilities. The meta_value of a user with the forum role 'spectator; is: a:2:{s:10:"subscriber";b:1;s:13:"bbp_spectator";b:1;}
Had the same requirement - to update the wp_capabilities field -
$wp_user_capabilities_arr = array(
"subscriber" => true,
"bbp_participant" => true
);
update_user_meta($wp_user_id, "wp_capabilities", $wp_user_capabilities_arr);
And after update the data looks like:
a:2:{s:10:"subscriber";b:1;s:15:"bbp_participant";b:1;}

Wordpress: Change user role conditionally

I am creating a Wordpress website for multi author and want to set user role as per article submission. Means if any user have 0-10 article they will go to Contributor role, if 11-30 will go to Author role if 31-100 will go to Editor role.
Also I want to make registration system where default registration group will be Subscriber. They will get a link into verification email like
If you want to become a Contributor please click on below link. (To submit an article you must have at least Contributor permission)
http:// link will be here ... this link automatically change user role from Subscriber to Contributor.
Hope I will get solution from you expert. I am posting this issue with lots of hope from you friends.
What you want to do is when they post their submission check to see how many posts they have authored and then change the role. So in your theme's functions.php file you'd need a hook that is like this.
add_action('publish_post', 'update_roles');
and then a function to update the roles.
function update_roles()
{
global $wpdb;
// Get the author
$author = wp_get_current_user();
// Not sure if $author and $u are the same object I suspect they are.
// so this may not be necessary, but I found this code elsewhere.
// You may be able to do without this and just replace $u with $author later in the code.
// Get post by author
$posts = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_author = " . $author->ID );
$numPost = count($posts);
// Do the checks to see if they have the roles and if not update them.
if($numPost > 0 && $numposts <= 10 && current_user_can('subscriber'))
{
// Remove role
$author->remove_role( 'subscriber' );
// Add role
$author->add_role( 'contributor' );
}
...... other conditions .......
}
Using SQL statements (Database Queries) to get at Wordpress data is not in accordance with Wordpress coding standards . See Wordpress Handbook
It would be better to use count_user_posts function
See Function on the codex

Resources