Email user on WordPress upgrade

I’m pretty new to WordPress and I’m trying to customize my theme to send an email to myself when an upgrade is required. I’d rather not use a plugin as this means I have to install it for each WordPress site. The code below is based from the update notifier plugin.

add_action('check_updates_daily', 'check_updates');

function check_updates_daily() {
    if (!wp_next_scheduled('check_updates')) {
        wp_schedule_event(time(), 'daily', 'check_updates');
    }
}

function check_updates() {

    $update_core = get_site_transient( 'update_core' );
    if (!empty($update_core) && isset($update_core->updates) && is_array($update_core->updates)
            && isset($update_core->updates[0]->response) && 'upgrade' == $update_core->updates[0]->response)
     {
        $newversion = $update_core->updates[0]->current;
        $oldversion = $update_core->version_checked;
        $blogurl = esc_url( home_url() );
        $message = "It's time to update the version of WordPress running at $blogurl from version $oldversion to $newversion.nn";

        // don't let $wp_version mangling plugins mess this up
        if (!preg_match( '/^(d+.)?(d+.)?(d+)$/', $oldversion)) {
            include( ABSPATH . WPINC . '/version.php' );
            $message = $wp_version == $newversion ? '' : "It's time to update the version of WordPress running at $blogurl from version $wp_version to $newversion.nn";
        }       
    }       

    //Send email
    if (!empty($message)) {     

        $subject = apply_filters( 'updatenotifier_subject', 'Updates are available for '.get_bloginfo('name').'.');

        wp_mail('email@email.com', $subject, $message);     
    }
}

I did change it to check hourly to test if it worked but I didn’t receive any emails I also scheduled it to just send an email without checking for updates and this also didn’t work.

Read More

Any help appreciated, thanks =D

Related posts

Leave a Reply

2 comments

  1. I forked the above plugin you mentioned as WP Updates Notifier

    Maybe you want to look at that instead?

    If you want to include it in your theme all you do is copy the plugin file and place in your theme functions file instead.

    If you need further help modifying this plugin leave a comment and I’ll see what I can do.

  2. Whether you use an already available plugin or write your own plugin, you’ll still need to install it on every WP site you set up. Don’t waste time re-inventing the wheel, just use the plugin you already found.