Latest TweetsPlugin update! Disable Gutenberg v1.5 adds whitelist options to always enable Block Editor on any post :) m0n.co/wpdg
Perishable Press

Activate WordPress Plugins via the Database

Recently a reader named Chris asked, “how can we turn ON a plugin from the database?” He mentioned reading my previous article, Quickly Disable or Enable All WordPress Plugins via the Database, but for circumstantial slash technical reasons needed to do the opposite and enable a plugin directly via the WordPress database. I thought it was an interesting question that might actually be useful to discuss here at Perishable Press.

Disclaimer

This tutorial explains how to enable plugins via the database, but it is not recommended that you do it on any live/production site. The technique is meant for experimenting and/or maybe if there is an emergency situation like getting hacked or whatever. The best and recommended way to enable plugins is via the Plugins screen in the WP Admin Area. If you activate plugins any other way, they may not function properly.

First: Understand

Before even touching your database, it is important to understand what you are doing. That way you are prepared if anything unexpected happens. Also, if the database contains any important data, it is recommended that you make good working backups, again, just in case.

For this tutorial, we are going to modify an option in the WordPress options table. The option name is active_plugins, and the value is a serialized array that contains information about which plugins currently are active.

By modifying the serialized active_plugins array, we can specify exactly which plugins are active. So that’s the basic idea, now let’s zoom in and see how it’s done. First we’ll walk through the process in general, and then later we’ll look at a specific example.

General technique

To activate any installed plugin, follow these steps:

Step 1: Copy the active_plugins array

Using a tool such as phpMyAdmin, enter the following SQL command:

SELECT * FROM wp_options WHERE option_name = 'active_plugins'

Remember to edit the default table prefix, wp_, to match your own configuration. After executing the query, copy the value of the active_plugins option. Depending on how many plugins you have, the copied value should look something like this:

a:10:{i:0;s:19:"akismet/akismet.php";i:3;s:45:"dashboard-widgets-suite/dashboard-widgets.php";i:4;s:33:"disable-embeds/disable-embeds.php";i:5;s:33:"disable-emojis/disable-emojis.php";i:6;s:43:"disable-wp-rest-api/disable-wp-rest-api.php";i:7;s:36:"google-sitemap-generator/sitemap.php";i:8;s:37:"simple-ajax-chat/simple-ajax-chat.php";i:9;s:45:"simple-local-avatars/simple-local-avatars.php";i:10;s:27:"updraftplus/updraftplus.php";i:11;s:45:"user-submitted-posts/user-submitted-posts.php";}

If that looks strange to you, join the club. But guess what, it’s just an ordinary serialized array. For this particular site, the array shows that there are 10 plugins currently active. We want to add another, so there will be a total of 11 active plugins.

Step 2: Unfold the active_plugins array

Next we want to “unfold” the active_plugins array, so that it looks like something like this:

a:10:{
	i:0;s:19:"akismet/akismet.php";
	i:1;s:45:"dashboard-widgets-suite/dashboard-widgets.php";
	i:2;s:33:"disable-embeds/disable-embeds.php";
	i:3;s:33:"disable-emojis/disable-emojis.php";
	i:4;s:43:"disable-wp-rest-api/disable-wp-rest-api.php";
	i:5;s:36:"google-sitemap-generator/sitemap.php";
	i:6;s:37:"simple-ajax-chat/simple-ajax-chat.php";
	i:7;s:45:"simple-local-avatars/simple-local-avatars.php";
	i:8;s:27:"updraftplus/updraftplus.php";
	i:9;s:45:"user-submitted-posts/user-submitted-posts.php";
}

Now things are beginning to make sense. Notice each item in the array represents one active plugin. For each item, the array index is denoted by i:n;, where n is some unique integer. Similarly, the array value begins with s:x:, where x is the number of characters contained in the quoted value, for example:

i:9;s:45:"user-submitted-posts/user-submitted-posts.php";

This is the last item in the array. It has an index of 9, and a string s value that is 45 characters in length: user-submitted-posts/user-submitted-posts.php. So one of the active plugins is User Submitted Posts.

Step 3: Add another plugin to the array

At this point, we want to add another plugin to the active_plugins array. To do so, we first prepare an array item, for example:

i:10;s:33:"my-plugin-name/my-plugin-name.php";

Here we have increased the index by 1, so we now have i:10;. And then for the value, we counted the number of characters in our plugin “slug”, which is the name of the plugin directory followed by a forward slash and the name of the main plugin file.

After preparing the array item, we want to add it to the array like so:

a:10:{
	i:0;s:19:"akismet/akismet.php";
	i:1;s:45:"dashboard-widgets-suite/dashboard-widgets.php";
	i:2;s:33:"disable-embeds/disable-embeds.php";
	i:3;s:33:"disable-emojis/disable-emojis.php";
	i:4;s:43:"disable-wp-rest-api/disable-wp-rest-api.php";
	i:5;s:36:"google-sitemap-generator/sitemap.php";
	i:6;s:37:"simple-ajax-chat/simple-ajax-chat.php";
	i:7;s:45:"simple-local-avatars/simple-local-avatars.php";
	i:8;s:27:"updraftplus/updraftplus.php";
	i:9;s:45:"user-submitted-posts/user-submitted-posts.php";
	i:10;s:33:"my-plugin-name/my-plugin-name.php";
}

Notice the index values are all unique and in ascending order. And the values all look good, correct syntax, and so forth. But now that we’ve added another item to the array, the array count a:10 is off by one. So because there now are 11 items in the array, we need to make one more slight modification:

a:11:{
	i:0;s:19:"akismet/akismet.php";
	i:1;s:45:"dashboard-widgets-suite/dashboard-widgets.php";
	i:2;s:33:"disable-embeds/disable-embeds.php";
	i:3;s:33:"disable-emojis/disable-emojis.php";
	i:4;s:43:"disable-wp-rest-api/disable-wp-rest-api.php";
	i:5;s:36:"google-sitemap-generator/sitemap.php";
	i:6;s:37:"simple-ajax-chat/simple-ajax-chat.php";
	i:7;s:45:"simple-local-avatars/simple-local-avatars.php";
	i:8;s:27:"updraftplus/updraftplus.php";
	i:9;s:45:"user-submitted-posts/user-submitted-posts.php";
	i:10;s:33:"my-plugin-name/my-plugin-name.php";
}

Perfect. Now the array is complete, but we still need to remove all of the whitespace that we added when we unfolded the array a few steps ago. The fastest way to remove all spaces is to use a free online tool like this one. After removing all whitespace from our serialized array, we have this:

a:11:{i:0;s:19:"akismet/akismet.php";i:1;s:45:"dashboard-widgets-suite/dashboard-widgets.php";i:2;s:33:"disable-embeds/disable-embeds.php";i:3;s:33:"disable-emojis/disable-emojis.php";i:4;s:43:"disable-wp-rest-api/disable-wp-rest-api.php";i:5;s:36:"google-sitemap-generator/sitemap.php";i:6;s:37:"simple-ajax-chat/simple-ajax-chat.php";i:7;s:45:"simple-local-avatars/simple-local-avatars.php";i:8;s:27:"updraftplus/updraftplus.php";i:9;s:45:"user-submitted-posts/user-submitted-posts.php";i:10;s:33:"my-plugin-name/my-plugin-name.php";}

All packed and ready for the database.

Step 4: Update the active_plugins array

Finally, we can return to the WordPress database and update the active_plugins array with our modified version. After the replacement has been made, we can visit the Plugins screen in the WP Admin Area, where we should see our new plugin active along with the 10 originally active plugins.

Troubleshooting

After updating the active_plugins array, if we find that ZERO plugins are active, then there was a mistake in the serialized array, either syntax, or spelling, or path name, or character count, misplaced or missing colon or semicolon, whatever. Serialized arrays must be exactly perfect or WordPress will empty the array and deactivate all plugins. So if you return to the Plugins screen to find all plugins disabled, or some other unexpected result, try again with the array, making sure that syntax is 100% correct.

Real-world example

Now that we’ve covered how to activate WordPress plugins via the database in general, let’s look at a specific (and quick) example. Let’s say that I want to activate Banhammer on a site that has a couple of other plugins already active. First I copy the value of the active_plugins option from the database:

a:2:{i:0;s:39:"block-bad-queries/block-bad-queries.php";i:1;s:45:"dashboard-widgets-suite/dashboard-widgets.php";}

I then prepare an array item for the Banhammer plugin:

i:2;s:23:"banhammer/banhammer.php";

So here the index 2 is because this will will be the third item in the array (remember array indexing begins at zero). Then for the value, s because the file path is a string that contains 23 characters, hence the 23. Also notice zero white spaces; we’re just going to insert that line directly into the active-plugins array, like so:

a:3:{i:0;s:39:"block-bad-queries/block-bad-queries.php";i:1;s:45:"dashboard-widgets-suite/dashboard-widgets.php";i:2;s:23:"banhammer/banhammer.php";}

Sanity checking everything looks good, I can now replace/update the active_plugins array in the options table. Done. Revisiting the Plugin screen in the WP Admin Area (and I actually did this while writing this tutorial), Banhammer is activated along with the other two plugins. Easy peasy, once you understand how it works.

Busta move

This tutorial shows you how to activate any WordPress plugin via the database. For those not familiar with serialized arrays, the process may seem overly complicated. But in reality, it’s just updating an array in the database. The only tricky part is making sure the syntax and logic is correct for the serialized array.

Jeff Starr
About the Author Jeff Starr = Web Developer. Security Specialist. WordPress Buff.
Archives
[ Comments are closed for this post ]