5

I want to restrict my users to be able to comment only once per post but be able to reply to existing comments. How can I do that?

I found this code which will do the half trick. With this code the comment form is hidden if they already comment in that post but they cant reply to existing comments.

<?php
global $current_user;
$args = array('user_id' => $current_user->ID);
$usercomment = get_comments($args);
if(count($usercomment) >= 1){
    echo 'disabled';
} else {
    comment_form();
}
?>

2 Answers 2

3

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!!!

2
  • Whereabouts in comments.php do I insert the code?
    – Pete
    Apr 17, 2016 at 2:20
  • Does anyone know where to add this exactly?
    – Pete
    Mar 17, 2018 at 10:37
0

If it hasn't to be "hacker-safe" you can realize it via JS. Display comment_form everytime, also if the user has written an comment. But render and unvisible HTML-Element (like an div with "has_written_comment" as ID).

Then on JS check whether an Element with this ID exists and hide the comment-form, until the "answer"-button is pressed. Then display the form. On "Abort answering" hide the form again ^^

I think you have to do it via JS unless you disable the JS-code for answer-buttons.

7
  • 1
    I don't really care about"hack-safe" as the malicious comments will be caught up by WordPress engine. What is the code for that? I dont know how to do it. Sep 25, 2014 at 12:14
  • @lippoliv, do you have a JS solution?
    – Pete
    Apr 10, 2016 at 12:58
  • @Pete This is quiet a view years late... I don't know what I did there. I think most Themes support that now, don't they?
    – lippoliv
    Apr 10, 2016 at 20:22
  • No themes don't do what the question was asking.
    – Pete
    Apr 11, 2016 at 8:53
  • Hey Pete, sorry I don't saw the condition for "one comment per user per post". Did you tested Mikes solution?
    – lippoliv
    Apr 11, 2016 at 12:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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