【问题标题】:Delete older users by role按角色删除旧用户
【发布时间】:2014-01-30 23:52:30
【问题描述】:

我想为 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 );

【问题讨论】:

  • 您不能在查询$query 中添加日期/时间字段吗?我不知道 wordpress 的数据库结构,但我相信它应该可以工作

标签: php wordpress


【解决方案1】:

在 WHERE 子句中使用 SELECT 将帮助您实现所需的目标(删除超过 X 天的用户)。 根据您的需要调整代码(阅读内联 cmets 进行解释)。之后,在插件文件夹中创建一个文件并将其命名为 delete_users.php。然后在里面添加以下代码,保存并从 WP Dashboard 将其作为插件激活。

删除_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 );

?>

【讨论】:

    猜你喜欢
    • 2019-08-29
    • 2019-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-28
    • 2015-12-17
    • 1970-01-01
    相关资源
    最近更新 更多