How to auto-upgrade my plugin?

The question:

How do I have my plugin pop up with that New version available. Upgrade Automatically dialog to appear when my plugin has a new version? Specifically for plugins not hosted on the WP.org repository.

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

This library integrates auto-updates for privately hosted plugins. Looks great.

Method 2

The easiest way I can think of to do this is to have your plugin “phone home” to check a URL on your website that returns the current version of your plugin.

That way, your plugin (installed on another website) can check its version against the “current” version on your website to see if it’s the same or newer.

Edit: sample code

I should point out before you implement this: this will add a banner across the top of the screen. If you’re just looking to have your plugin show an update available in the menu down the left, I think it automagically does that when you upload the new version to the WordPress repository. If you do want the banner across the top, proceed.

I would create a template specifically for this page so that the header and footer were not included:

<?php

    // Template Name: Bare Template

    while (have_posts()) : the_post();
        the_content();
    endwhile;

?>

That way, all that will get printed is the page content, which is good for what we’re doing.

Next, set up a page that uses this template, say, “latest-plugin-version”. If you view this page in your browser now, all it should say is the text from the page with no extra html.

In your plugin, create a function that prints out your notice. I’ve used inline styles in the example, you can use classes if you want.

function yourpluginname_check_for_new_version() {

    /* You probably shouldn't check for updates more than once a day, 
    for everyone's bandwidth's sake. */

    $last_check = get_option('yourpluginname_lastcheck');
    if ( $last_check + 86400 > time() ) { return; }

    // If we're still here, check your site for a new version.

    $current_version = get_option('yourpluginname_version');
    $latest_version = file_get_contents('http://www.yourdomain.com/latest-plugin-version/');

    if ( $current_version != $latest_version ) {
        ?>
        <div style="background: #FFDDDD; color: red; width: 600px; 
                    margin: 20px auto; padding: 10px; text-align: center;
                    border: 2px red solid;">
            There's a new version of MY PLUGIN available! You should upgrade now.
        </div>
        <?php
    }

    // Log that we've checked for an update now.
    update_option('yourpluginname_lastcheck', time());

}

And then, to tie it in so that it runs the function:

add_action('admin_notices', 'yourpluginname_check_for_new_version');

Now, in your installation function, you should add an option for the version number to be saved in the user’s WordPress:

update_option( 'yourpluginname_version', '2.0' );

You can use update_option instead of add_option, as it will add the option if it doesn’t already exist.

That should about do it.

Method 3

There is a free plugin that allows autoupgrade for privately hosted plugins:
https://github.com/meglio/wp-upgrademe


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment