Last year I helped a friend develop a simpleWordPress CRM application, my friend has been using it with good results and has accumulated many customers now. Now a friend is encountering a problem: he always forgets to visit customers regularly, and the customer visits lack regularity, making return visit management inconvenient. He wants to add a reminder for regular return visits in this CRM application.
The specific requirements are as follows:
- After adding a customer, remind customer service to return to visit the customer every 1 week, 3 weeks, and one and a half months
- After one and a half months, remind customer service to return to visit the customer every three months.
Solution ideas for adding regular return visit reminders in WordPress CRM
The requirement is very simple and the solution is very clear. It can be implemented directly using the WordPress task system. When adding a customer, add several scheduled tasks that are executed only once to send the single reminder in requirement 1. At the same time, add a scheduled task that is executed cyclically every 3 months to send a reminder every 3 months. used in the code belowWEEK_IN_SECONDSandMONTH_IN_SECONDSBoth are constants used by WordPress to represent seconds.
Add scheduled task
add_action( 'wp_insert_post', function ( $post_id, $post, $update ) {
// 排除不需要提醒的操作
if ( wp_is_post_revision( $post_id ) || wp_is_post_autosave( $post_id ) || $update ) {
return;
}
$weeks = [ 1, 3, 6 ];
// 添加单次计划任务
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 ] );
}
}
// 添加循环计划任务
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 scheduled task interval
Since WordPress’s default scheduled task interval is not defined every three months, we need to add a custom scheduled task interval, which can be any time.
add_filter( 'cron_schedules', function ( $schedules ) {
// 每 3 月
$schedules[ 'three_monthly' ] = [
'display' => __( '三个月', 'enter' ),
'interval' => MONTH_IN_SECONDS * 3,
];
return $schedules;
} );
When a scheduled task occurs, send an email reminder
Here is the actual operation of the CRM system to send reminders when scheduled tasks occur. It can be sending emails to customer service, or sending site messages, text messages, or even pushing to WeChat, Android clients, etc. In fact, in the customer’s CRM system, we have added an on-site messaging system to facilitate customer service to view and process return visit messages.
add_action( 'remind_communicate', function ( $post_id ) {
// 获取客户信息和消息接收者
$post = get_post( $post_id );
$owner = get_user_by( 'ID', $post->post_author );
// 消息内容
$subject = '请回访客户' . carbon_get_post_meta( $post_id, 'name' );
$content = '客户「' . carbon_get_post_meta( $post_id, 'name' ) . '」于「' . carbon_get_post_meta( $post_id, 'deal_date' ) . '」签订了合同。是时候回访一下了。';
// 发送邮件给用户
wp_mail( $owner->user_email, $subject, $content );
} );
When deleting a customer, delete reminders for returning customers
After a user deletes a customer, the scheduled task to remind a return visit is no longer needed. We need to automatically delete the scheduled task corresponding to the customer when deleting the customer to prevent the CRM system from sending useless return visit reminders.
add_action( 'delete_post', function ( $post_id ) {
wp_clear_scheduled_hook( 'remind_communicate', [ 'post_id' => $post_id ] );
} );
WordPress checks and executes scheduled tasks when the user accesses the page. When no user accesses the system, reminders will not be sent in time because the scheduled tasks are not triggered. We need toTrigger WordPress scheduled tasks through operating system scheduled tasks. With this system, customers no longer have to worry about forgetting to return to a customer. They can go to the CRM application background every day to check for return visit message reminders. If there are new reminders, they can just return to the customer and add a return visit record.
