0

I want an email to be sent to a specific list of email addresses if/when there have been no posts on the blog within the past 1 week. Any ideas on how I could do this?

3

You'll need to set up a cron job that checks once a day to see if the latest post is more than seven days old.

So, some where in a plugin file. Schedule a new event. Then hook into that event. Grab the post date, turn it into a unix time stamp, and compare that with the current time.

<?php
register_activation_hook( __FILE__, 'wpse29671_activation' );
function wpse29671_activation()
{
    wp_schedule_event( time(), 'daily', 'wpse29671_cron' );
}

add_action( 'wpse29671_cron', 'wpse29671_maybe_send_email' );
function wpse29671_maybe_send_email()
{
    // get the latest post
    $posts = get_posts( array( 'numberposts' => 1 ) );
    if( ! $posts ) return;

    // Latest posts date as a unix timestamp
    $latest = strtotime( $posts[0]->post_date );

    // how long has it been?
    $diff = ( time() - $latest ) / ( 60 * 60 * 24 );

    // if it has been more than 7 days, send the email
    if( $diff >= 7 )
    {
        wp_mail( 'youremail@yoursite.com', 'Better Write a Post!', 'Hey, you should go write a blog post or something' );
    }
}

As a plugin: https://gist.github.com/1246814

Alternatively, you could use DateTime objects to do the time comparison or just use this plugin (but it's way more fun to figure it out yourself, just sayin').

  • 1
    As a caveat, this will only work if you have consistent traffic to the site. This is a pseudo cron rather than a true scheduled server crontab job. It is solely based on page views to check if the wp-cron event should run. – Brian Fegter Sep 28 '11 at 2:24
  • 1
    A simpler date check: if(strtotime($posts[0]->post_date) < strtotime('-7 days')) ... – onetrickpony Sep 28 '11 at 2:47

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.