Advertisement
kayart

Untitled

May 8th, 2023
879
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.87 KB | None | 0 0
  1. function get_user_comments_count_for_last_month() {
  2.     if ( ! is_user_logged_in() ) {
  3.         return 0;
  4.     }
  5.    
  6.     $user_id = get_current_user_id();
  7.     $last_month = date( 'Y-m-d', strtotime( '-1 month' ) );
  8.  
  9.     // Get user comments number for the last month
  10.     $args = array(
  11.         'user_id' => $user_id,
  12.         'status' => 'approve',
  13.         'date_query' => array(
  14.             array(
  15.                 'after' => $last_month
  16.             )
  17.         ),
  18.         'count' => true
  19.     );
  20.  
  21.     $comments_count = get_comments( $args );
  22.    
  23.     return $comments_count;
  24. }
  25.  
  26. add_shortcode( 'comments-counter', 'render_comments_counter' );
  27. function render_comments_counter() {
  28.     $comments_count = get_user_comments_count_for_last_month();
  29.    
  30.     $message = '';
  31.    
  32.     if ( $comments_count < 5 ) {
  33.         $diff = 5 - $comments_count;
  34.         $message = $diff . ' Comments still needed';
  35.     } else {
  36.         $message = 'You are elegible for a giveaway!';
  37.     }
  38.    
  39.     return $message;
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement