Use Scheduled Tasks to Add Recurring Follow-Up Reminders in a WordPress CRM

Last year I built a simple WordPress CRM application for a friend. It has worked well, and over time it has accumulated quite a few customers. But then a new problem appeared: people kept forgetting to follow up with customers on schedule, and the follow-up process became irregular and hard to manage. The obvious next step was to add recurring reminders to the CRM.

The requirements were straightforward:

  • After a customer is added, remind customer service to follow up after 1 week, 3 weeks, and 1.5 months.
  • After 1.5 months, continue sending follow-up reminders every 3 months.

How to approach recurring follow-up reminders in a WordPress CRM

The requirement is simple, and so is the solution. We can implement it with WordPress’s scheduling system. When a customer is created, we schedule several one-time events for the first group of reminders, and then we schedule a recurring event that runs every three months. In the code below, WEEK_IN_SECONDS and MONTH_IN_SECONDS are WordPress constants that represent time in seconds.

Add the scheduled tasks

add_action( 'wp_insert_post', function ( $post_id, $post, $update ) {

   // Exclude operations that should not trigger reminders.
   if ( wp_is_post_revision( $post_id ) || wp_is_post_autosave( $post_id ) || $update ) {
      return;
   }

   $weeks = [ 1, 3, 6 ];
   
   // Add one-time scheduled tasks.
   if ( $post->post_type === 'client' ) {
      foreach ( $weeks as $week ) {
         wp_schedule_single_event( current_time( 'timestamp', 1 ) + WEEK_IN_SECONDS * $week, 'remind_communicate', [ 'post_id' => $post_id ] );
      }
   }

   // Add a recurring scheduled task.
   if ( ! wp_next_scheduled( 'remind_communicate' ) && $post->post_type == 'client' ) {
      wp_schedule_event( current_time( 'timestamp' ), 'six_monthly', 'remind_communicate', [ 'post_id' => $post_id ] );
   }

}, 10, 3 );

Add the custom schedule interval

WordPress does not define a built-in schedule that runs once every three months, so we need to register our own interval. That interval can be any amount of time we need.

add_filter( 'cron_schedules', function ( $schedules ) {

   // Every 3 months
   $schedules[ 'three_monthly' ] = [
      'display'  => __( '三个月', 'enter' ),
      'interval' => MONTH_IN_SECONDS * 3,
   ];

   return $schedules;
} );

Send the reminder when the scheduled task runs

This is the actual reminder action that the CRM performs when the event fires. In practice, you could send an email to customer service, create an internal notification, send an SMS, or even push a message to WeChat or an Android app. In our CRM system, we added an internal inbox so customer service staff could view and process follow-up messages conveniently.

add_action( 'remind_communicate', function ( $post_id ) {

   // Get customer data and the message recipient.
   $post  = get_post( $post_id );
   $owner = get_user_by( 'ID', $post->post_author );

   // Message content.
   $subject = '请回访客户' . carbon_get_post_meta( $post_id, 'name' );
   $content = '客户「' . carbon_get_post_meta( $post_id, 'name' ) . '」于「' . carbon_get_post_meta( $post_id, 'deal_date' ) . '」签订了合同。是时候回访一下了。';

   // Send the email to the user.
   wp_mail( $owner->user_email, $subject, $content );
} );

Delete the reminder when the customer is deleted

Once a customer is deleted, the follow-up reminders for that customer are no longer needed. We should clear the scheduled tasks automatically when that customer record is removed so the CRM does not keep sending useless reminders.

add_action( 'delete_post', function ( $post_id ) {
   wp_clear_scheduled_hook( 'remind_communicate', [ 'post_id' => $post_id ] );
} );

WordPress checks and runs scheduled tasks when someone visits the site. If nobody visits for a while, reminders may not be sent on time because the scheduled task is never triggered. For a production CRM, it is better to trigger WordPress Cron from the operating system’s own scheduler. Once that is in place, the team no longer has to worry about forgetting a customer follow-up. They can simply check the CRM inbox each day, handle any new reminders, and record the follow-up activity.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *