One comment per user per post but be able to reply to existing comments

So after some time I done exactly what I wanted and I thought it would be nice to share.

So in functions.php add

function c_parent_comment_counter($pid,$uid){

    global $wpdb;
    $query = "SELECT COUNT(comment_post_id) AS count FROM $wpdb->comments WHERE <code>comment_approved</code> = 1 AND <code>comment_post_ID</code> = $pid AND <code>user_id</code> = $uid AND <code>comment_parent</code> = 0";
    $parents = $wpdb->get_row($query);
    return $parents->count;
}

and in comments.php

global $current_user, $post;
$number_of_parents = c_parent_comment_counter($post->ID,$current_user->ID);
echo "parents: ".$number_of_parents;
if ( $number_of_parents >= 1 ) {
    echo '<nav class="withcomment">';
    comment_form( array( 'title_reply' => __( 'Reply' ) ) );
    echo '</nav>';
} else {
    echo '<span class="withoutcomment">' . comment_form( array( 'title_reply' => __( 'Your opinion' ) ) ) . '</span>';
}
?>

in style.css

.withcomment {
    display: none;
}

This will count how many parent comments a user added and if its one or more it will hide the form. if its 0 it will show the form. but when you click the reply button the form will show and you will be able to reply to any comment.
THIS IS NOT HACK PROOF!!!

Leave a Comment