I'm currently working on a editorial heavy WordPress site, and one thing that I'd like to implement is a functionality that sends someone of a specific role (in this case contributor/author) an email alert when they haven't created/published any new content in a set period (for example 20 days).

However, I have no idea what would be the best way to attempt this. Would it make sense to attempt something like this with Hooks? However, I'm concerned about the fact that these notifications will then be dependent on the loading of the site/dashboard or to a specific action that would trigger this check?

  • Sounds like a great opportunity to write a custom plugin. Yes, you would be relying on at least one hook to determine if any user acct meets your inactivity definition. You can avoid your last concern by either registering with WP Cron or hooking to init. As long as you have a visitor to the site, it would not matter if an admin connected or not. – jdm2112 Nov 3 '15 at 13:55
  • @jdm2112 Great! I'd definitely look into WP Cron. – Schalk Venter Nov 5 '15 at 6:56
  • It is important to note that WP Cron is dependent on visitors to the site. Without visitors, WP does not run anything. – jdm2112 Nov 5 '15 at 14:23

There are three main parts to your question:

  1. Log the user last login time
  2. Schedule a cron event
  3. Send email if contributors and authors haven't logged in within 20 days

The first step is adding user meta to track the last login. WordPress does not track the last time a user logs in, so we're going to have to do it manually.

//* Add action on login to update the last_login user meta
add_action( 'wp_login', 'wpse_207422_user_last_login', 10, 2 );
function wpse_207422_user_last_login( $user_login, $user ) {
  update_user_meta( $user->ID, 'last_login', time() );

Next, we check to see if our event is scheduled and if it isn't, schedule it. This should only run once, but you never know, another plugin might accidentally disable it.

//* Schedule a daily cron event
if( ! wp_next_scheduled( 'wpse_207422_inactivity_reminder' ) ) {
  wp_schedule_event( time(), 'daily', 'wpse_207422_inactivity_reminder' );

Finally, add an action for our cron event. The callback will perform a user query targeting contributors and authors and the meta value last_login. The comparison makes sure that the last_login is older than 20 days.

//* Add action to daily cron event
add_action( 'wpse_207422_inactivity_reminder', 'wpse_207422_inactivity_reminder' );
function wpse_207422_inactivity_reminder() {
  //* Get the contributors and authors who haven't logged in in 20 days
  $users = new \WP_User_Query( [
    'role'         => [ 'contributor', 'author', ],
    'meta_key'     => 'last_login',
    'meta_value'   => strtotime( '-20 days' ),
    'meta_compare' => '<',
  ] );
  foreach( $users->get_results() as $user ) {
      __( 'Inactivity Notice', 'wpse-207422' ),
      __( 'We notice you have not logged in for 20 days.' ,'wpse-207422' )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.