简体   繁体   中英

WordPress : Send email after 25 day for author of post

I need to convert any post in custom post type banner after 30 days to draft, I do that by below function (action), But i need after 25 days when author post this post to send email to him that told him that post we convert to draft.

How can do that ?

function ads_show_expire(){
  global $wpdb;
  $result = $wpdb->get_results("SELECT * FROM wp_posts WHERE post_type = 'banner' AND post_status = 'publish'");
  if( !empty($result)) foreach ($result as $a){
      $show_time = get_the_time("Y-m-d", $a->ID );
      $show_time=date("Y-m-d", strtotime("+30 day", strtotime($show_time)));
      if ( $show_time < date("Y-m-d")){//compare 1 year after post with current time
          $my_post = array();
          $my_post['ID'] = $a->ID;
          $my_post['post_status'] = 'draft';
          wp_update_post( $my_post );
      }
  } // end foreach
}

add_action( 'init', 'ads_show_expire' );

You need to set cronjob which will execute every day and send mail and convert that post to draft. The code which you have write will be execute every time when any user will visit the page. So the best solution is to create cron-job for that.

Where condition to get 25 days old post $where .= " AND date(post_date) = '" . date('Ym-d', strtotime('-25 days')) . "'";

 `if ( $show_time < date("Y-m-d")){//compare 1 year after post with current time
      $my_post = array();
      $my_post['ID'] = $a->ID;
      $my_post['post_status'] = 'draft';
      wp_update_post( $my_post );
      $user_info = get_userdata($a->post_author);
      $email = $user_info->user_email;
  }`

It may help you,

  • You should set cron job file using the cpanel, in that you have to write above code and also fetch the post date and compare that date with current date, it is match than write the email function to send the mail to told him that post we convert to draft. Below is the sample code of cron file that you should put in root folder of your wordpress folder for your reference:

<?php // FOR DATABASE OPERATION include('../wp-load.php'); // FOR GET_USERDATA FUNCTION & $WPDB include('../wp-includes/pluggable.php'); wp-includes/author-template.php //sample email code $headers = "MIME-Version: 1.0" . "\\r\\n"; $headers .= "Content-type:text/html;charset=UTF-8" . "\\r\\n"; $headers .= 'From: <test@gmail.com>' . "\\r\\n"; $headers .= 'Cc: test@gmail.com' . "\\r\\n"; $to = "test@gmail.com"; $subject = "subject text"; $meesage = "test"; $sentmail = mail($to,$subject,$message,$headers); if($sentmail) echo $msg = "Mail has been sent successfully..."; else echo $msg = "Mail has been failed..."; Before that you should your above function code.

You can get author email using below function:

<?php the_author_email(); ?> <?php the_author_email(); ?> OR <?php get_the_author_meta( 'user_email', $userID ); ?> <?php get_the_author_meta( 'user_email', $userID ); ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM