How to Write a Plugin Update/Upgrade Routine

When creating a new version of your plugin, it is a common need to perform some tasks to complete the upgrade, especially related to the database. Many of you have seen similar functionality when updating WordPress itself. For some version upgrades, WordPress displays a screen and asks your permission to upgrade the database before you can resume using the dashboard.

Sometimes as developers we might change how the plugin stores its data in the database, for example, so we need to write some code that performs the changes as soon as the plugin is upgraded.

What’s the best of handling that?

Well, as on most occasions, we can follow the way WordPress itself does it, as described in the WordPress Codebase Handbook.

The idea is to have a db version stored in a global variable in your plugin. You will also have another db version number stored in the database. Whenever you make any changes to the database schema for your plugin, update the global variable. When the plugin is updated, that will generate a trigger because the global variable won’t match the value stored in the database.

Once we have the trigger we can then execute the code to perform the db upgrade operation, which in the example below is contained in myplugin_install().

So, the code would look like this:

In the example above we are just creating a new table, but of course you can insert any SQL operation you want there.

Here’s a more generic example:

If you came here looking for ways in which you can automatically send plugin update notifications to your users (especially if you own a premium plugin), check out our post about integrating plugin update notifcations within your plugin.

If you enjoyed this post, make sure to subscribe to WPMayor’s RSS feed.

About Jean Galea

Jean Galea is a WordPress developer, entrepreneur and padel player. He is the founder of WP Mayor, the plugins WP RSS Aggregator and EDD Bookings, as well as the podcast. His personal blog can be found at

Related Articles

Leave a Reply