Installing and updating custom tables in WordPress is done within the same function so you don’t have to worry about writing altering queries. In this tutorial, we will create a class that will help us work with installing and updating our own custom tables in WordPress.

In the previous tutorial, we introduced the idea of custom tables and what can be done with them as well. The code that we will do here and in other tutorials of this series, can be also added in any plugin, so I won’t be making a complete plugin out of it.

We will also namespace our classes here, but if you don’t use namespaces in your solution, you can remove that and be sure to rename the classes with a unique name.

Installing Custom Tables in WordPress

Before we learn how to update our custom tables in WordPress, we need first to install them 🙂 Let’s now have a look at how that is done.

First, we have a class that has both install and activate methods. The latter method will be used only once, when the plugin is being activated.

The activation method can hold a bunch of other functions and code that make sense to be called when the plugin is activated. This can include functionalities such as installing first pages (WooCommerce installs store and my account pages) or flushing the rewrite rules because you are including new routes.

Inside of the install method, we need to include a file to get the dbDelta function and pass the table schemas to install them.

I’ve included a meta table here as well to show you how to register those meta tables. But we will go further into them in a future tutorial.

Activating the plugin and installing custom tables

Let’s now learn where to call those methods in our main plugin. You can, of course, call this elsewhere if you have already a structure of files and folders.

We call the function register_activation_hook to call the activate method in our installer class. And we should now also make sure that we register our meta table as well.

How we name the table in $wpdb is of most importance if we are going to use the metadata functions. But let’s leave that for an upcoming tutorial.

Updating Custom Tables in WordPress

We need to prepare our installer class for the whole update flow. In theory, we will do this:

  1. Check version from DB
  2. Check current plugin version from code
  3. If DB version is lower, start the update process

Let’s open the installer class now and add this:

The attribute $db_updates will be an array where the key will be the version for which we have functions and the value will be an array of functions to be called. That way you can have smaller update functions for the same version.

Calling update functions

Let’s now create the update method for it. This method will expect a $from version. This version is the DB version that is stored so we know from which version are we updating. That way, if we have update functions for version 1.3.0 such as in the example above and our DB version is 1.2.0, those functions will be called.

In this method, we go through each of the update versions and check if that version is higher or lower than the one provided. If it’s higher, we call all those functions (if they exist).

Checking if update is needed

We need to have a way to check if we need to update the tables or anything else within our plugin.

This method will get the current version from the database (or return the default one 0.0.1 if none is found). Then it will compare with the current version in the code. If those are not the same, that is, if the current one is higher than the one from DB, it will call the update method.

Here, we also call an action so if we have some integrations, those can be hooked on that and process their own update flow.

So, when do we do those checks? We hook it on the init action. Since this version option is autoloaded, it’s available immediately.

Conclusion

When you have an update flow within your plugin, it is much easier to work on new versions and make sure that all new data inside of custom tables or in options that you store are updated to work with the latest version.

Have you implemented such update flow in your own solution? Let us know in the comments below.

Become a Sponsor

Posted by Igor Benic

Web Developer who mainly uses WordPress for projects. Working on various project through Codeable & Toptal. Author of several ebooks at https://leanpub.com/u/igorbenic.

Leave a reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.