问题
我有一个基于用户的网站,所以当用户deletes 他的个人资料中,他/她的评论总是清晰可见。评论中没有名字和空白的头像。如果用户删除其个人资料,是否有任何方法可以删除用户的评论?如何删除?是否有某种函数?这真是个奇怪的问题。
评论中没有名字和空白的头像。如果用户删除其个人资料,是否有任何方法可以删除用户的评论?如何删除?是否有某种函数?这真是个奇怪的问题。
+1@toscho的评论。这将中断注释中嵌套的注释/对话。应禁用设置中的嵌套注释。
请尝试此代码。请注意,未对此进行测试
add_action( \'delete_user\', \'my_delete_user\');
function my_delete_user($user_id) {
$user = get_user_by(\'id\', $user_id);
// delete comment with that user email
$comments = get_comments(\'author_email=\'.$user->user_email);
foreach($comments as $comment) :
wp_delete_comment($comment->$comment_id, true);
endforeach;
// delete comment with that user id
$comments = get_comments(\'user_id=\'.$user_id);
foreach($comments as $comment) :
wp_delete_comment($comment->$comment_id, true);
endforeach;
}
Modify the coments of a deleted user
当注册用户被删除时,可以删除他所有的评论(见上面我的答案),但在大多数情况下,这是一个非常愚蠢的想法。如果你do not 使用嵌套注释的唯一效果是,如果某些注释是对已删除注释的回复,则它们没有意义。如果使用嵌套注释,则它最终会显示在注释树的有线显示中。在这种情况下,更好的策略是重命名注释作者名称,并保持注释内容不变。
add_action( \'delete_user\', \'modify_deleted_user_comments\', 5, 1 );
function modify_deleted_user_comments( $user_id = 0 ){
if( 0 === $user_id )
return;
$user = get_userdata( $user_id );
$new_author_name = apply_filter( \'modify_deleted_user_comments_new_author_name\', \'Deleted Author\' );
$new_comment_content = apply_filter( \'modify_deleted_user_comments_new_comment\', \'\' );
$new_user_id = apply_filter( \'modify_deleted_user_comments_new_user_id\', 0 );
global $wpdb;
$sql_erasing_comment_content = $wpdb->prepare(
"UPDATE {$wpdb->comments}
SET comment_author = \'%s\', comment_content = \'%s\', user_id = %d
WHERE comment_author = \'%s\' OR comment_author_email = \'%s\' OR user_id = %d;
",
$new_author_name,
$new_comment_content,
$new_user_id,
$user->display_name,
$user->user_email,
$user->ID
);
$sql_keep_comment_content = $wpdb->prepare(
"UPDATE {$wpdb->comments}
SET comment_author = \'%s\', user_id = %d
WHERE comment_author = \'%s\' OR comment_author_email = \'%s\' OR user_id = %d;
",
$new_author_name,
$new_user_id,
$user->display_name,
$user->user_email,
$user->ID
);
$res = $wpdb->query( $sql_keep_comment_content );
return $res;
}
此解决方案将not 删除任何评论。它将仅替换注释作者名称或注释作者名称以及注释内容。How it works
该函数将获取将被删除的用户的ID。它将根据用户ID获取用户的显示名称和电子邮件。mysql查询获取all 与显示名称匹配的注释or 电子邮件or ID并替换作者名称和用户ID(以及注释内容)。Caution!
一方面,该函数将修改所有注释。即使是这个,也是在用户未登录时进行的。另一方面,它可能会修改not 由将被删除的用户编写。E、 g.:Ben是注册用户,ID为5,电子邮件ben@hisblog.tld.Ben的所有评论都有user_id
注释表中的5。有一天Ben没有登录就写了一条评论user_id
共0个。如果我们只获取带有user_id
在5个用户中,我们怀念Ben未登录时的评论。我们只能通过他的显示名或电子邮件获取此评论。
Jerry也是一个注册用户,在发表评论之前,Jerry总是先登录。但还有另一个未注册的用户使用相同的显示名称“Jerry”。上述功能还将修改未注册Jerry的注释。这通常不会发生。
如果你想写一个插件来删除或修改被删除用户的评论,你需要一个很好的策略来避免遗漏评论或删除/修改非被删除用户的评论。您可以列出所有有疑问的评论并手动删除它们。或者只修改那些被删除的用户明确发表的评论,并遗漏一些评论。或者干脆修改所有可以获取的注释。
Conclusion
总而言之,这个问题没有简单的解决办法。它需要大量的编程和很好的策略来删除/修改评论。add_action( \'deleted_user\', 5, 1 );
function delete_user_comments( $user_id = 0 ){
if( 0 === $user_id )
return;
global $wpdb;
$res = $wpdb->query(
$wpdb->prepare( "DELETE FROM {$wpdb->comments} WHERE user_id = %d", $user_id )
);
return $res;
}
每次用户被删除时,他们的评论也会在数据库中被删除。我遵循了“ajaxify”wordpress评论的教程,其中不会重新加载页面:jQuery(\'document\').ready(function ($) { $(\'#commentform\').each(function () { var commentform = $(this); // find the comment form commentform.prepend(\'<div id=\"comment-status\"