Rob
Rob

Reputation: 6380

Send email if user hasn't logged in for X amount of time

Is there a way to do the following:

  1. Find out when a user last logged into Wordpress.
  2. If it's longer than X amount of time then send an email alert.

Essentially the idea is to encourage users to log in and update the website.

I've found this plugin which gives me the last login time. I can also use wp_mail(); to send emails but how would I trigger all that / continually check it the last login date?

Upvotes: 1

Views: 1811

Answers (3)

Steve Grunwell
Steve Grunwell

Reputation: 896

With the plugin you mentioned, you'll have a new database column in your wp_users table, which gives you a good starting point.

Next, as @bswatson mentioned, you can leverage WP Cron to run a task that checks for inactive users (if low site traffic is a concern, you might consider setting up WP Cron to use a real cron job). The daily task would look something like this:

  1. Collect all users from the database who haven't logged in in the last X days or more.

    • You might consider installing the plugin before rolling out the cron task, or risk emailing everyone.
    • As you probably don't want to spam people daily, I'd recommend adding a bit of user meta to indicate this person has been emailed already; you can filter out those users as you build your list of people to email, then delete that user meta when the user logs in.
    • Depending on the size of your WordPress site, you'll probably want to limit this to only a few users; it might make sense to email admins and editors (people with publishing capabilities) but maybe not authors/contributors/subscribers. If you need to email large numbers of people, I'd recommend batching (you can even register cron jobs from within cron jobs) to avoid sending out dozens/hundreds/thousands of emails at once.
  2. Loop through the users, emailing them using wp_mail() to nudge them to log in.

    • Once an email is sent, add a bit of user meta to prevent duplicate emails.

Good luck!

Upvotes: 1

bswatson
bswatson

Reputation: 467

WordPress has a built in system called WP Cron which mimics a system cron, but can be set up and configured directly in code. There are some drawbacks, such as needing traffic to kick of a task.

Here's a pretty thorough breakdown of how WP Cron works including some ways to workaround the issues.

Upvotes: 0

everis
everis

Reputation: 316

Well it depends on what options you have on your hosting. I don't know how to do it purely in WordPress (and I don't know if something like that is possible).

But I would use some cron job to run some small script. If you can use system cron, of course.

EDIT: I found this WP plugin. But as I suspected, it depends on users visiting your website. So if you want to send emails independently on your website traffic, then you need to use some OS solution.

Upvotes: 0

Related Questions