{"id":50,"date":"2018-11-21T10:32:13","date_gmt":"2018-11-21T02:32:13","guid":{"rendered":"https:\/\/www.intelliwolf.com\/?p=50"},"modified":"2021-09-15T08:16:18","modified_gmt":"2021-09-15T00:16:18","slug":"schedule-follow-up-emails-wordpress","status":"publish","type":"post","link":"https:\/\/www.intelliwolf.com\/schedule-follow-up-emails-wordpress\/","title":{"rendered":"How To Schedule Follow Up Emails In WordPress"},"content":{"rendered":"\n
On a recent project, I needed to have a series of emails following up new users, prompting them to setup their profile. I couldn't use Aweber, like I normally would, because the follow up emails had to stop when they completed their profile. That could happen at any time<\/p>\n\n\n\n
So how did I schedule follow up emails in WordPress?<\/p>\n\n\n\n
You need to create a list of people for following up. I already had a process that hooked into user registration, which you can read about here<\/a>. So I built upon that code. If you want to do the same, see the code on that post, then add the code from new_user_followup_registration<\/em> set out below at the end of<\/p>\n\n\n\n For this tutorial, I'll assume you are using standalone code.<\/p>\n\n\n\n Put the following code into your functions.php<\/em>:<\/p>\n\n\n\n The way this works is it runs just before the new user is sent the welcome email.<\/p>\n\n\n\n You can run a conditional if you just want to send the follow up to certain users (maybe only subscribers or a particular level of member).<\/p>\n\n\n\n You could also change the filter to something else, if you have a different trigger for initializing the sequence.<\/p>\n\n\n\n The list of users to follow up is stored in the wp_options<\/em> table under \"new_user_followup<\/em>\".<\/p>\n\n\n\n There's a check in case there are no current users to follow up.<\/p>\n\n\n\n Then we build a new array entry, pulling in the ID, email, time of registration and stage of follow up. You can certainly pull 'email'<\/em> and 'registered'<\/em> from the user table when you do your later checks, but I chose to have it all together for speed and independence.<\/p>\n\n\n\n Finally we store the updated array with the new user in the wp_options<\/em> table, then let the email continue processing. Make sure you leave that return<\/em> in there or the user won't get their welcome email.<\/p>\n\n\n\n While you could build this functionality in your functions.php<\/em> file or a separate file in your template, I chose to build this as a plugin.<\/p>\n\n\n\n The main reason for this was so I didn't have to mess about with setting or unsetting the cron events.<\/p>\n\n\n\n It's probably not entirely best practice to combine an entry in functions.php<\/em> with a plugin. I decided to do it on this project because the code in functions.php<\/em> is not dependent on the plugin. As we were already doing a custom function for the initial emails, it didn't make sense to split the code into two places.<\/p>\n\n\n\n The entire plugin code is as follows:<\/p>\n\n\n\n You can use this as-is. Just put it into a file called<\/p>\n\n\n\n Then zip the folder and upload it as a regular plugin.<\/p>\n\n\n\n If this is your first custom WordPress plugin, congratulations! At its most basic, that's all there really is to it.<\/p>\n\n\n\n Let's break the code down, shall we?<\/p>\n\n\n\nif (get_user_meta($user->ID, 'membership_user_role', true) == 'job_seeker') {}<\/code><\/pre>\n\n\n\n
add_filter('wp_new_user_notification_email', 'new_user_followup_registration', 10, 3);\n\nfunction new_user_followup_registration($wp_new_user_notification_email, $user, $blogname)\n{\n $new_users = get_option('new_user_followup');\n if (empty($new_users)) {\n $new_users = array();\n }\n \n $new_users[$user->user_login] = array(\n 'ID' => $user->ID,\n 'email' => $user->user_email,\n 'registered' => time(),\n 'last_email' => 'registration'\n );\n update_option('new_user_followup', $new_users);\n \n return $wp_new_user_notification_email;\n}<\/code><\/pre>\n\n\n\n
Create a Basic Plugin for Follow Up<\/h2>\n\n\n\n
<?php\n\/**\n * Plugin Name: New User Email Followup\n * Plugin URI: https:\/\/intelliwolf.com.au\n * Description: Follow Up Sequence For New User Registration\n * Author: Mike Haydon\n * Author URI: https:\/\/intelliwolf.com.au\n * Version: 1.0\n *\/\n\nif(!defined('ABSPATH'))\n exit;\n\n\/* Set cron for followup profile completion emails *\/\nregister_activation_hook(__FILE__, 'new_user_followup_activation');\n\nfunction new_user_followup_activation()\n{\n if (!wp_next_scheduled('new_user_followup')) {\n wp_schedule_event(time(), 'daily', 'new_user_followup');\n }\n}\n\nregister_deactivation_hook(__FILE__, 'new_user_followup_deactivation');\n\nfunction new_user_followup_deactivation()\n{\n wp_clear_scheduled_hook('new_user_followup');\n}\n\nadd_action('new_user_followup', 'user_followup');\n\nfunction user_followup() {\n $new_users = get_option('new_user_followup');\n if (empty($new_users)) {\n \/\/ nothing to process\n die();\n }\n\n $day = 60 * 60 * 24;\n $current_time = time();\n\n foreach ($new_users as $user => &$info)\n {\n if (empty($info['ID']) or empty($info['email'])) {\n unset($new_users[$user]);\n continue;\n }\n\n $new_user_profile_id = get_user_meta($info['ID'], 'member_profile', true);\n if (!empty($new_user_profile_id)) {\n unset($new_users[$user]);\n continue;\n }\n\n $days_since_registration = ($current_time - $info['registered']) \/ $day;\n if ($days_since_registration < 2) {\n continue;\n }\n\n if ($days_since_registration > 12) {\n wp_delete_user($info['ID']);\n unset($new_users[$user]);\n continue;\n }\n elseif ($days_since_registration > 7) {\n $email_to_send = 'final reminder';\n }\n elseif ($days_since_registration > 4) {\n $email_to_send = 'second reminder';\n }\n elseif ($days_since_registration >= 2) {\n $email_to_send = 'first reminder';\n }\n\n if (!empty($info['last_email']) and ($info['last_email'] == $email_to_send)) {\n continue;\n }\n\n $info['last_email'] = $email_to_send;\n $new_user_email = new_user_email($email_to_send);\n if (empty($new_user_email['subject']) or empty($new_user_email['message'])) {\n continue;\n }\n\n $headers = array(\"Content-Type: text\/html; charset=UTF-8\", \"From: Website <info@website.com>\");\n\n wp_mail($info['email'], $new_user_email['subject'], $new_user_email['message'], $headers);\n }\n\n update_option('new_user_followup', $new_users);\n}\n\nfunction new_user_email($stage)\n{\n $subject = '';\n $message = '';\n switch($stage) {\n case 'first reminder':\n $subject = \"Please Complete Your Profile\";\n $message = \"Thank you for your interest etc\";\n break;\n case 'second reminder':\n $subject = \"Reminder: Please Complete Your Profile\";\n $message = \"Thank you for your interest etc\";\n break;\n case 'final reminder':\n $subject = \"Final Reminder: Please Complete Your Profile\";\n $message = \"Thank you for your interest etc\";\n break;\n }\n return array(\n 'subject' => $subject,\n 'message' => $message\n );\n}\n?><\/code><\/pre>\n\n\n\n
\/new-user-email-followup\/new-user-email-followup.php<\/code><\/pre>\n\n\n\n
Initializing the Plugin<\/h2>\n\n\n\n