0

I use a WordPress theme that awards users points (kept track of in user meta-data) for posting comments. However, they are not awarded points for replying to a comment.

When I move a comment to the trash, one point is deducted from the comment's author via the following code:

// Remove 1 point if their comment is removed
function deleteAPointFromUser( $comment_id ) {
    $comment = get_comment( $comment_id );
    $authorid = $comment->user_id;
    $currentPointNumber = get_user_meta( $authorid, 'points', true );

    // Decrement comment author's "points" by 1
    update_user_meta( $authorid, 'points', $currentPointNumber - 1 ); 
}
add_action( 'trash_comment', 'deleteAPointFromUser' );

The problem is that when I delete replies to a comment, a point is deducted from the reply's author (even though that author never received a point for replying in the first place).

I want to remove comment replies without deducting a point from their authoring users.

  • Anyone got any ideas? It's really annoying. Is there are way to exclude the "comment reply"? – dkl Jan 28 '14 at 19:09
0

Simply check if the comment has a parent before decrementing points. Reading the Codex entry for the get_comment() function, you'll note that in the manner you use the function you will be returned an object containing keys that correspond to the column names of the wp_comments table. Viewing the wp_comments scehma, note that there is a column called comment_parent that contains the the post ID of the comment's parent, or defaults to 0 if the comment has no parent. Therefore, you can achieve the desired effect via the following:

function deleteAPointFromUser( $comment_id ) {
    $comment = get_comment( $comment_id );

    // Only decrement user 'points' if the comment being deleted has no parent comment.
    if( $comment->comment_parent == 0 ) {
        $authorid = $comment->user_id;
        $currentPointNumber = get_user_meta( $authorid, 'points', true );

        update_user_meta( $authorid, 'points', $currentPointNumber - 1 );
    }
}
add_action( 'trash_comment', 'deleteAPointFromUser' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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