I need a function to limit the same user to leave max 1 comment every minute on my wordpress website (all posts).

  • 2
    how do you define what is a "user" Sep 3 '16 at 13:33
  • What are you hoping to achieve by doing this? Are you trying to avoid spam?
    – Tom J Nowell
    Sep 3 '16 at 13:51

Place the following into a custom plugin or into your child theme's functions file:

add_action("wp_insert_comment", function() { 
    global $current_user;
    if ( !$current_user->ID ) return;
    update_user_meta( $current_user->ID, "se_last_commented", time() );

add_action("init", function() { 
    if ( "wp-comments-post.php" == $GLOBALS['pagenow'] ) {
        global $current_user;
        if ( !$current_user->ID ) return;
        $last_commented = (int) get_user_meta( $current_user->ID, "se_last_commented", 1 );
        if ( !$last_commented ) return; // no time recorded
        if ( (time() - $last_commented) > DAY_IN_SECONDS ) {
            $date = date( "H -1-, i -2-, s -3-", DAY_IN_SECONDS - (time() - $last_commented) );
            $date = str_replace( array('-1-','-2-','-3-'), array('hr','min','sec'), $date );
            wp_die(sprintf( '<strong>ERROR:</strong> You have <u>%s</u> left until you can be able to post a comment.', $date ));
}, 0);

Basically what it does is it records the time integer of last time a user has posted a comment, and a user here means a logged-in WP user, and when any user submits a comment, WP will automatically check for last time this user has posted a comment and if it was longer than a day (DAY_IN_SECONDS) then the process will be allowed, otherwise the user will see a death screen where they're notified how long they'll have to wait until they can be able to post a comment.

Hope that helps.

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.