0

I want to limit the number of comments per user (registered or not) per day. I used the codes of the answer in the following page but it did not work. I put the first code in the Functions.php and replace the second code with comment_form(); in Comments.php. this is the codes I used:

User can post only one comment per day on one page

this is the code in Functions.php:

function is_user_limit_reached( $limit = 5, $time = 1, $ids = array() ){
    global $wpdb;

    // Cast to array
    ! is_array( $limited ) AND $limited = array( $limited );

    // Generate SQL clause
    $ids = ! empty( $ids )
        ? sprintf( " and comment_post_ID in (%s)", join( ",", $ids ) )
        : "";

    // Rows: user_id, comment_date, comment_post_ID
    $count = $wpdb->query( $wpdb->prepare( 
        "select count(*)
        from {$wpdb->comments} 
        where user_id=%d 
            and comment_date >= date_sub( now(), interval %d day )
            %s",
        get_current_user_id(),
        $time,
        $ids
    ) );

    return $count >= $limit;
}

the code which is replaced with comment_form(); in Comments.php:

if ( is_user_limit_reached(
    10,                      // allowed comments per day
    7,                       // in the last 7 days
    array( 1, 35, 97, 1148 ) // array of post IDs which have a limit
    ) )
{
    echo 'Your limit is already reached. Come back tomorrow';
}
else
{
    comment_form();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.