0

I have a function which, when a post is published inside a CPT after three days the page is redirected to another URL/PERMALINK.

This is the code:

add_action('template_redirect', 'check_the_date_for_stats');

function check_the_date_for_stats() {
  if ( is_singular('debate') ) { // adjust myCPT with your real cpt name
    $pl = get_permalink( get_queried_object() ); // permalink
    $is_stats = array_key_exists( 'stats', $GLOBALS['wp_query']->query ); // is stats?
    $is_cm = array_key_exists( 'comments', $GLOBALS['wp_query']->query ); // is comments?
    $ts = mysql2date('Ymd', get_queried_object()->post_date_gmt ); // post day
    $gone = ($ts + 3) < gmdate('Ymd'); // more than 3 days gone?
    if ( $gone && ( ! $is_stats && ! $is_cm ) ) {
       // more than 3 days gone and not is stats => redirect to stats
       wp_redirect( trailingslashit($pl) . '/stats' );
       exit();
    } elseif( ! $gone && ( $is_stats || $is_cm ) ) {
       // we are in 3 days frame and trying to access to stats => redirect to post
       wp_redirect( $pl );
       exit();
    }
  }
}

Is it possible, after 3 days are gone to send an email with a standard message to all registered users, if so how can I do this ? I know this function:

<?php wp_mail( $to, $subject, $message, $headers, $attachments ); ?> but I don't know how can I link this with my function to send an email to all registered users after three days have passed.

Can someone give me some hints on how can I achieve this ? Thanks !

0

Don't do this if you have too many registered users on the site. Below is the edited portion from your code.

} elseif( ! $gone && ( $is_stats || $is_cm ) ) {
   // we are in 3 days frame and trying to access to stats => redirect to post

   // Start the code to send the emails
   $subject = '3 days have passed';
   $users = get_users();
   foreach($users as $user) {
      $message = 'Hello '.$user->display_name.', '.PHP_EOL;
      $message .= 'The 3 days have past since the post.';
      wp_mail($user->user_email, $subject, $message);
   }
   // End the code to send the emails

   wp_redirect( $pl );
   exit();
}

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.