Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Problem

I have a user based site, so when user deletes his profile somehow his/her comments stay visible.

There is no name and blank avatar with the comment. Is there any way to delete the comments of a user if the user deletes his/her profile and how? Is there a function of somehow? This really is a strange problem.

share|improve this question
1  
In a normal WordPress installation (singel and multisite), users can not delete their own profile. Please tell us what kind of WordPress installation you have ad what plugins are installed for user management. –  Ralf912 Nov 26 '12 at 21:49
    
@Ralf912 new Wordpress 3.4.2 installation of Wordpress with plugins: "DELETE ME" installed + "WP-Ajaxify-Comments". I tried the below function but the comment remains, the blank image without the deleted users name and his/hers comment visible. Thank YOu for the help in advance! :) –  jimilesku Nov 27 '12 at 21:32
    
does your comments nested? –  ifdion Nov 28 '12 at 23:44
add comment

3 Answers

up vote 1 down vote accepted

+1 @toscho 's comment. This will break nested comment / conversation in comment. You should disable nested comments in the Settings.

Try this code. Note that this is not tested

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;

}
share|improve this answer
1  
$comments = array_merge( get_comments('author_email='.$user->user_email), get_comments('user_id='.$user_id) ); One foreach is enough ;) –  Ralf912 Nov 29 '12 at 22:15
    
Yes, that would be better –  ifdion Nov 29 '12 at 22:24
add comment

Modify the coments of a deleted user

It is possible to delete all comments of a registered user when he will be deleted (see my answer above), but in most cases it is a very stupid idea. If you do not use nested comments, than the only effect is, that some comments have no sense if they are replys on a deleted comment.

If you use nested comments, than it end up in a very wired display of the comment tree. In this case it is a better strategy to rename the comments author name and left the comment content untouched.

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;

}

This solution will not delete any comments. It will only replace the comment author name or the comment author name and the comment content.

How it works

The function will get the ID of the user who will be deleted. Based on the users ID, it will fetch the users display name and e-mail. The mysql query fetch all comments which match with the display name or the e-mail or the ID and replace the author name and user ID (and the comment content).

Caution!

On one hand the function will modify really all comments. Even this one, which are made when the user is not logged in. On the other hand, maybe it will modify comments which are not written by the user who will be deleted.

E.g.: Ben is a registered user with ID 5 and e-mail ben@hisblog.tld. All comments fro Ben have the user_id 5 in the comments table. One day Ben wrote a comment without logging in, this comment got a user_id of 0. If we only fetch comments with the user_id of 5, we miss Ben's comments when he is not logged in. We can fetch this comments only by his display name or e-mail.

Jerry is also a registered user and Jerry always log in before he comment. But there is another unregistered user who use the same display name 'Jerry'. The function above will also modify the comments of the unregistered Jerry. This should normally not happen.

If you want to write a plugin which delete or modify the comments of a deleted user, you will need a very good strategy to avoid missing comments or deleting/modify comments which are not from the deleted user. You can list all questionable comments and delete them by hand. Or modify only those comments, which a definitively made by the deleted user and miss some comments. Or be brutal and simply modify all comments you can fetch.

Conclusion

All in all there is no simple solution for this problem. It need a lot of programming and a very good strategy for deleting/modifing the comments.

share|improve this answer
    
If you just set the user_id to zero then the comment should still have the proper user's name and email and attribution and such, I think. Simpler. –  Otto Nov 28 '12 at 22:58
add comment
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;

}

Every time a user will be deleted, their comments are also deleted in database.

share|improve this answer
1  
Might break existing comment threads in interesting ways. :) –  toscho Nov 27 '12 at 5:27
    
@toscho Yes, it does. But I am sure that this question does not refers on WordPress. Because WordPress does not delete comments when a user is deleted. This question must be related to a plugin. Somehow it makes sense when the comments are blanked out but not be fully deleted. –  Ralf912 Nov 27 '12 at 16:13
    
@toscho I have WordPress installed latest version, and I am experimenting and having frontend author page with ability for user to delete their account. I'm using a plugin and You are right, it's called: "Delete Me". I would love if the comment thread would not break if possible with the function? Any help would be greatly appreciated guys :) –  jimilesku Nov 27 '12 at 21:27
1  
@jimilesku You would have to re-parent all orphaned comments. That’s the point where you leave plain database operations and enter the wide field of semantics (commentators might quote each other). Honestly, that is the proof that your idea is not that good. :) Accept Ralf’s answer, but do not use it. Replace the comment text with deleted by author or something similar, and keep the threads intact. –  toscho Nov 28 '12 at 6:36
1  
@jimilesku First: Toscho did not write function. Second: WordPress - Stack Exchange is for WordPress developers This means, you won't get a complete solution for copy&paste use. If you are a developer, explore and develop at your own. I gave you a lot of hints, tips and code. If you are not a developer, hire one. –  Ralf912 Nov 28 '12 at 21:49
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.