1

Let's say I have a plugin that's currently at version 1.1, and I am releasing version 1.2. When you run the 1.2 update, a script will run to add new options, columns, tables, etc.

A month later, 1.3 is released. Do I still need to include the update scripts in version 1.2 so people can update the plugin from v1.1 directly to v1.3?

Is there a way for WordPress to automatically download v1.2, run the update scripts, and then download v1.3, or do I need to include some sort of flag in v1.3 that says "if upgrading from 1.1 or below, run the v1.2 update scripts first"? In other words, is there a way to flag v1.3 of my plugin and say, "you need v1.2 or higher of this plugin for this update to work"?

  • Also, as I release subsequent versions (i.e. v1.4, 1.5, etc), do I need to include update scripts from all the previous versions? In other words, does v1.5 have to have update scripts from v1.4, v1.3, and v1.2 in order for someone to update the plugin directly from v1.1 -> v1.5? – rafiki_rafi May 12 '14 at 15:47
  • 1
    In addition to the answers below. 2 years after I released an update, I removed the update code. Shortly afterwards I had someone whose update failed because they were using a very very early version. Moral of the story: put your update scripts 'out of the way' if you like, but you'll be keeping them indefinitely ;) – Stephen Harris May 12 '14 at 17:03
2

Do I still need to include the update scripts in version 1.2 so people can update the plugin from v1.1 directly to v1.3?

Yes.

Is there a way for WordPress to automatically download v1.2, run the update scripts, and then download v1.3...

No.

...is there a way to flag v1.3 of my plugin and say, "you need v1.2 or higher of this plugin for this update to work"?

Typically, a plugin author will store the version of their plugin in the options table. Then, either on init or admin_init, compare it to the currently running version (and subsequently run any appropriate updates if required, and update the version in the options table).

function wpse_144165_admin_init() {
    // If your plugin will fail on the front-end if not upgraded (typically due to
    // a drastic change in code), you'll need to perform this operation on "init".

    wpse_144165_upgrade();
}

add_action( 'admin_init', 'wpse_144165_admin_init' );

function wpse_144165_upgrade() {
    if ( $version = get_option( 'my_version' ) ) {
        if ( version_compare( $version, MY_CURRENT_VERSION ) === -1 ) {
            // Perform upgrade(s), typically in another function/class that you extend
            // over versions to support updates from 1.0 upwards.

            update_option( 'my_version', MY_CURRENT_VERSION );
        }
    }
}

function wpse_144165_activation() {
    // Do pre-upgrade stuff.

    wpse_144165_upgrade();

    // Do post-upgrade stuff.
}

register_activation_hook( __file__, 'wpse_144165_activation' );

This technique of version checking accomodates for users who still upgrade via good old FTP, since WordPress will never fire any re-activation hooks.

  • 1
    Just a comment on the last sentence: WP never fires any activation hook even when a plug-in is updated by clicking "upgrade". – Stephen Harris May 12 '14 at 17:00
  • 1
    I was under the impression that it deactivated it, updated the filesystem, then re-activated it? – TheDeadMedic May 12 '14 at 17:23
  • 1
    It does, but without triggering any of the associated hooks. – Stephen Harris May 12 '14 at 17:27
  • I feel enlightened :) – TheDeadMedic May 13 '14 at 11:29
1

If WordPress were to download each intermediate version to see if it contains an update script you would be downloading a lot more than if your update function just included the cumulative changes. It would be much better to store the current version number in an option. Then in the update routine check the stored version number and compare against each step that needs to happen like so:

if ( '1.1' > $stored_version ) {
  // updates to bring us up to 1.1
}
if ( '1.2' > $stored_version ) {
  // updates for 1.2
}

Or you can do something really fancy with switch.

The main idea here is that if you were at version 1.0 both blocks execute. If 1.1 only the second fires. And you can keep adding steps after that which will be executed in order to bring you up from wherever you are to where you need to be.

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.