• Resolved SialoS

    (@sialos)


    Hello and thank you for your time,

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

    I found this code which disable the comment form if the user has comment, but it’s not working. The code it’s 3 years old.

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

    Thank you

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter SialoS

    (@sialos)

    Hello,

    I think i can do it if i show in both cases the comment form but hide it conditionally in the places that i want based on css classes.
    but i have problem with my syntax if you can help.
    this is my code:

    <?php
    global $current_user,$post;
    $args = array('user_id' => $current_user->ID,'post_id' => $post->ID);
    $usercomment = get_comments($args);
    if(count($usercomment) >= 1){
     <nav class="commentifrated">
    comment_form();
    </nav>
    } else {
    <span class="commentifnotrated"> comment_form(); </span>;
     }
    ?>

    and i get this error
    Parse error: syntax error, unexpected ‘<‘ in

    Moderator keesiemeijer

    (@keesiemeijer)

    Hi SialoS

    You cannot mix html and php. Try echoing the html

    <?php
    global $current_user, $post;
    $args = array( 'user_id' => $current_user->ID, 'post_id' => $post->ID );
    $usercomment = get_comments( $args );
    if ( count( $usercomment ) >= 1 ) {
    	echo '<nav class="commentifrated">';
    	comment_form();
    	echo '</nav>';
    } else {
    	echo '<span class="commentifnotrated">' . comment_form() . '</span>';
    }
    ?>

    Or use the alternative syntax:
    http://php.net/manual/en/control-structures.alternative-syntax.php

    <?php
    global $current_user, $post;
    $args = array( 'user_id' => $current_user->ID, 'post_id' => $post->ID );
    $usercomment = get_comments( $args );
    if ( count( $usercomment ) >= 1 ) : ?>
    	<nav class="commentifrated">
    		<?php comment_form(); ?>
    	</nav>
    <?php else : ?>
    	<span class="commentifnotrated"><?php comment_form(); ?></span>;
    <?php endif; ?>

    Thread Starter SialoS

    (@sialos)

    This is a solution with css trick .

    <?php
    global $current_user,$post;
    $args = array('user_id' => $current_user->ID,'post_id' => $post->ID);
    $usercomment = get_comments($args);
    if(count($usercomment) >= 1){?>
    <div class="withcomment">
    <?php
    comment_form();
    ?>
    </div>
    <?php } else { ?>
    <div class="withoutcomment">
    <?php
    comment_form();
    ?>
    </div>
    <?php }
    ?>

    and add this to your css

    .withcomment {
        display: none;
    }

    if someone knows something better please post

    Thread Starter SialoS

    (@sialos)

    So after some time i done exactly what i wanted and i thought it would be nice to share.
    @keesiemeijer Thank you for your help. i have changed the code based on your contribution.

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

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘comment once per post but reply freely’ is closed to new replies.