Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm pretty new to WordPress and i'm trying to customise my theme to send an email to myself when a 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.\n\n";

        // 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.\n\n";
        }       
    }       

    //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 recieve any emails I also secheduled it to just send an email without checking for updates and this also didn't work.

Any help appreciated, thanks =D

share|improve this question
    
did you check your spam? –  Ramkumar Jul 7 '11 at 9:51
    
Are you testing on a local environment? By default local set-ups don't have the ability to send emails out so you wont get the email notification. –  Brady Jul 7 '11 at 9:56
    
I'm testing on a hosted server and yes I checked my spam it doesn't appear to have sent at all. Thanks –  Elliott Jul 7 '11 at 10:01
add comment

2 Answers

up vote 0 down vote accepted

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.

share|improve this answer
    
I did basically copy the plugin file and put it into the functions file, although I modified it to remove some of the settings etc. Does wordpress not check if the plugin is activated before it is used? –  Elliott Jul 7 '11 at 10:02
    
yes you're right. In my plugin all that happens on plugin activation is to setup the cron. So you will need to use do_action("sc_wpun_enable_cron"); to setup the cron. –  Brady Jul 7 '11 at 10:09
    
You can also use do_action("sc_wpun_update_check") to manually kick off an update check there and then so you dont have to wait for the cron to trigger. –  Brady Jul 7 '11 at 10:11
    
Thanks will give that ago :) –  Elliott Jul 7 '11 at 11:48
    
I have just spent the last hour or so trying to get it working but with no luck. It works if I add updates[0]->response function outside the class (didn't add it all in comment) and then add the wp_email function. This sends me an email, but it doesnt work if its inside the class. I used do_action("sc_wpun_update_check") and added wp_email to the top of the class to email me but that didnt work either. Its like it isn't even calling the class? Thanks again. –  Elliott Jul 7 '11 at 15:18
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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