我想为WORDPRESS创建一个脚本,该脚本可以按超过20天的角色(例如“订阅者”)删除用户。我搜索了一个插件,但我找不到。
我发现此脚本会按角色删除用户,但我想删除具有相同角色的超过20天的用户。
<?php
define( 'WP_USE_THEMES', false );
require( './wp-load.php' );
$role = "subscriber"; // The role to kill
$reassign = 1; // The user that all posts will fall back to, other wise they will be deleted
$this_role = "[[:<:]]$role[[:>:]]";
$query = $wpdb->prepare( "SELEC T user_id FROM $wpdb->usermeta WHERE meta_key = '{$wpdb->prefix}capabilities' AND meta_value RLIKE %s", $this_role );
if ( $users_of_this_role = $wpdb->get_results( $query, ARRAY_N ) )
foreach ( $users_of_this_role as $user_id )
wp_delete_user( $user_id[0], $reassign );
答案 0 :(得分:2)
在WHERE子句中使用SELECT将帮助您实现所需的内容(删除超过X天数的用户)。 根据您的需要调整代码(阅读内联注释以获得解释)。之后,在插件的文件夹中创建一个文件,并将其命名为delete_users.php。然后在WP Dashboard中添加以下代码,保存并将其作为插件激活。
delete_users.php
<?php
/**
* Plugin Name: Delete old users
* Plugin URI: http://stackoverflow.com/users/395160/manolis
* Description: Delete users older than x amount of days
* Version: 1.0
* Author: Manolis
* Author URI: http://stackoverflow.com/users/395160/manolis
* License: GPL2
*/
if ( !defined('ABSPATH') )
define('ABSPATH', dirname(__FILE__) . '/');
global $wpdb;
require_once(ABSPATH.'/wp-admin/includes/user.php');
$role = "author"; // The role to kill
$reassign = 1; // The user that all posts will fall back to, other wise they will be deleted
$days = 20; // Calculated in days, how old a user should be in order to get deleted
$this_role = "[[:<:]]".$role."[[:>:]]";
$query = $wpdb->prepare("
SELECT user_id
FROM $wpdb->usermeta
WHERE meta_key = '{$wpdb->prefix}capabilities'
AND (SELECT user_id
FROM $wpdb->users
WHERE user_registered < NOW() - INTERVAL $days DAY
)
AND meta_value RLIKE %s", $this_role);
if ( $users_of_this_role = $wpdb->get_results( $query, ARRAY_N ) )
foreach( $users_of_this_role as $user_id )
wp_delete_user( $user_id[0], $reassign );
?>