4

I am using WordPress as a petition site and hacking the comments system to work as the petition signup. I would like to find out how I can limit a user to post only one comment per post. So far I can limit each user to one comment on the whole WordPress site as shown in the code below, but that's not what I am looking to accomplish.

Again, how can I limit each user to one comment per post?

   <?php
   /*
    * Single Petition Template
    * @package WordPress
    * @subpackage Twenty_Thirteen
    * @since Twenty Thirteen 1.0
    * template author: Facinet Toure
    * website URL: http://mongage.com
   */

 get_header(); ?>

 <div id="primary" class="content-area">
  <div id="content" class="site-content" role="main">
    <?php /* The loop */ ?>
    <?php while ( have_posts() ) : the_post(); ?>
    <?php //get_template_part( 'content', get_post_format() ); ?>
    <!-- post format -->
    <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
        <header class="entry-header">
            <?php if ( has_post_thumbnail() && ! post_password_required() ) : ?>
            <div class="entry-thumbnail">
                <?php the_post_thumbnail('full'); ?>
            </div>
            <?php endif; ?>
            <h1 class="entry-title">
                <?php the_title(); ?>
            </h1>
            <p><span class="rounded-corners alignleft"><?php echo get_avatar( $post->post_author, 74 ) ?></span>
                <span class="author"><?php the_author_meta( 'display_name' ); ?></span>
                <span> | </span>This post currently has
                <?php comments_number( 'no responses', 'one response', '% responses' ); ?>
                .</p>
        </header>
        <!-- .entry-header -->
        <div class="entry-content">
            <?php the_content( __( 'Continue reading <span class="meta-nav">&rarr;</span>', 'twentythirteen' ) ); ?>
            <?php wp_link_pages( array( 'before' => '<div class="page-links"><span class="page-links-title">' . __( 'Pages:', 'twentythirteen' ) . '</span>', 'after' => '</div>', 'link_before' => '<span>', 'link_after' => '</span>' ) ); ?>
        </div>
        <!-- .entry-content -->

        <footer class="entry-meta">
            <?php twentythirteen_entry_meta(); ?>
            <?php if ( comments_open() && ! is_single() ) : ?>
            <span class="comments-link">
            <?php comments_popup_link( '<span class="leave-reply">' . __( 'Sign the petition', 'twentythirteen' ) . '</span>', __( 'One comment so far', 'twentythirteen' ), __( 'View all % comments', 'twentythirteen' ) ); ?>
            </span><!-- .comments-link -->
            <?php endif; // comments_open() ?>
            <?php edit_post_link( __( 'Edit', 'twentythirteen' ), '<span class="edit-link">', '</span>' ); ?>
            <?php if ( is_single() && get_the_author_meta( 'description' ) && is_multi_author() ) : ?>
            <?php get_template_part( 'author-bio' ); ?>
            <?php endif; ?>
        </footer>
        <!-- .entry-meta --> 
    </article>
    <!-- #post -->

    <p> This post currently has
        <?php comments_number( 'no responses', 'one response', '% responses' ); ?>
        . </p>
    <?php twentythirteen_post_nav(); ?>
    <ol reversed class="commentlist">
        <?php
    //Gather comments for a specific page/post 
    $comments = get_comments(array(
        'post_id' => $post->ID,
        'status' => 'approve' //Change this to the type of comments to be displayed
    ));

    //Display the list of comments
    wp_list_comments(array(
        'per_page' => -1, //Allow comment pagination
        'reverse_top_level' => false, //Show the latest comments at the top of the list,
        'max_depth' => '1',
        'avatar_size' => 0,
    ), $comments);
?>
    </ol>
    <?php // check if user has previously commented the post
        global $current_user;
        if ( !is_user_logged_in() ) {
        echo 'you must be logged in to sign the petition';
        } else {
        $args = array('user_id' => $current_user->ID);
        $usercomment = get_comments($args);
        if(count($usercomment) >= 1) { 
            echo '<p>Vous avez déjà signé cette pétition. S\'il vous plaît partager avec votre famille et vos amis</p>';

        } else { comment_form(); 
        } 
    }?>
    <?php endwhile; ?>
   </div>
   <!-- #content --> 
   </div>
  <!-- #primary -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>
  • What if you pass in post_id as part or the $args array you send to get_comments. $args['post_id']=get_the_ID(); – czerspalace Mar 13 '14 at 6:42
  • did not work. What I need here is to check the number of comment from current user based on the selected post to be commented. – cmsdeployed Mar 13 '14 at 7:14
6
// Check if user has previously commented the post.
global $current_user, $post;

if ( ! is_user_logged_in() ) {
    // Show the comment form if the user is not logged in.
    comment_form();
} else { // The user is logged in...

    // Get the comments for the logged in user.
    $usercomment = get_comments( array (
            'user_id' => $current_user->ID,
            'post_id' => $post->ID,
    ) );

    // If the user has commented, output a message.
    if ( $usercomment ) { 
        echo '<p>Vous avez déjà signé cette pétition. S\'il vous plaît partager avec votre famille et vos amis</p>';
    } else { // Otherwise, show the comment form.
        comment_form(); 
    }
}

Now the code is checking if the user has previously commented on that specific post instead of the whole site. Hope others will find it helpful.

  • How could this be adapted to test if the current user has commented 0-Nth times on the current post? (where we can add the number of comments we want tested) – Pete Feb 26 '18 at 9:51
  • 1
    @Pete you would pass in 'count' => true to the get_comments array which would return the number of times the user has commented – Alex Older Feb 26 '18 at 10:28

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.