Note -- 正如Kaiser在评论中正确指出的那样,多站点在站点之间共享用户。如果您删除了一个,则会将她从您网络中的所有站点中删除。如果这不是你的意图,那么调查一下remove_user_from_blog()
.
而不是
wp_delete_user()
, 使用
wpmu_delete_user()
. 正在查看
the source code,
wpmu_delete_user()
首先检查所涉及的用户是否是网络中任何站点上的用户,如果是,则将其删除,清除与其帐户关联的所有链接和元数据,然后最终按预期删除该用户。
This WordPress support thread 是什么指引我朝着这个方向。
do_action( \'wpmu_delete_user\', $id );
$blogs = get_blogs_of_user( $id );
if ( ! empty( $blogs ) ) {
foreach ( $blogs as $blog ) {
switch_to_blog( $blog->userblog_id );
remove_user_from_blog( $id, $blog->userblog_id );
$post_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_author = %d", $id ) );
foreach ( (array) $post_ids as $post_id ) {
wp_delete_post( $post_id );
}
// Clean links
$link_ids = $wpdb->get_col( $wpdb->prepare( "SELECT link_id FROM $wpdb->links WHERE link_owner = %d", $id ) );
if ( $link_ids ) {
foreach ( $link_ids as $link_id )
wp_delete_link( $link_id );
}
restore_current_blog();
}
}
因此,以编程方式删除用户时的默认操作是从她作为用户的每个站点(博客)中查找并删除她的帖子。然而,有一个操作挂钩就在前面运行,方便地命名为
wpmu_delete_user
. 因此,在删除之前,您应该能够通过编程方式重新分配要删除的用户的帖子:
add_action( \'wpmu_delete_user\', \'wpse130705_reassign_posts\' );
function wpse130705_reassign_posts( $id ) {
// $id should contain the user\'s ID
$blogs = get_blogs_of_user( $id );
$assign_to = 99; // some other user\'s ID; you\'ll have to work out what this will be
if( ! empty( $blogs ) ) {
foreach( $blogs as $blog ) {
switch_to_blog( $blog->userblog_id );
$post_ids = $wpdb->get_col(
$wpdb->prepare(
"SELECT ID FROM $wpdb->posts WHERE post_author=%d", $id
)
);
if( $post_ids ) {
foreach( $post_ids as $post_id ) {
$wpdb->update(
$table = $wpdb->posts,
$data = array( \'post_author\' => $assign_to ),
$where = array( \'post_author\' => $id ),
$format = \'%d\',
$where_format = \'%d\'
);
}
}
restore_current_blog();
}
}
}