WordPress Dev 101: Setting Activation/Deactivation options [updated]

by Chris K on October 3, 2010


When you are writing a WordPress plugin, you are more than likely going to have some sort of settings involved. These options are stored in the wp_options (or wp_blog id_options for multi site users) table of your database. Most of today’s WordPress users aren’t privy to the fact that every plugin they install, probably has something within their database taking up space. Some of the sites I’ve seen have over 50 plugins installed that, at one point or another, were activated. That’s 50+ possible rows in the database of options that may not be needed anymore if the plugin developer didn’t remove the options from the database upon deactivation.

The goal of every plugin should be optimal user experience without affecting the overall performance of the users site. In order to provide this experience you should have a set of ‘default’ settings for your plugin. These option settings should be set upon activation. This provides users with immediate gratification, and who doesn’t like instant gratification? Likewise, if someone chooses to deactivate your plugin and delete it, you should no longer be leaving an imprint on their configuration.

Today we’ll cover two hooks you can use to setup default options AND remove options upon deactivation.

This post is assuming you already have setup your plugin to have options and contains an admin area for saving these options. If you are just starting with creating your plugin you can view the WordPress.org page for Writing a Plugin to get started

Setting up the Hooks

The first section of your plugin should be the hooks you are going to use for your plugin. I’ll take an example from my previous post about WordPress Hook. This example shows how to hook an action on the front end for visitors to your site. Plugins however, get activated from the admin section of the site so we’ll need to check that we are in the ‘wp-admin’ area with the ‘is_admin()’. You will need more than just these two hooks to properly create a plugin, I am only focusing on the related ones at this time:

if ( is_admin() ) { //Adds admin verification
register_uninstall_hook('plugin-folder/main-plugin-file.php', 'de_register_settings_something-unique');
register_activation_hook('plugin-folder/main-plugin-file.php', 'pre_register_settings_something-unique');
}

This hook has two arguments, the first being the path to the plugin file when viewing from the ‘wp-content/plugins’ directory. It should just be the folder name, and then the file name. The 2nd argument is the function to run upon activation/deactivation. This function name needs to be unique to your plugin as having two plugins with the same function name will cause a site to produce errors and possibly not load. This will be included in your plugin file.

Default options upon activation

To set option values upon activation, we use the update_option() function from WordPress. This function is preferred over the add_option() function as it add the option if it does not exist, so it essentially functions as both an update and an add function.

There are two arguments for the update_option() function which are the option_name you have selected for the option, and the value you wish to set it to upon activation of your plugin.

function pre_register_settings_something-unique() { // Upon Activation set defaults
update_option( 'option_name', 'option_value' );
}
Removing options upon Deactivation

If someone chooses to deactivate your plugin, you should be removing your options from the database to do this we call the function that is hooked with the register_deactivation_hook() we specified above which should read like the following:

function de_register_settings_something-unique() { // Delete options from db on deactivate
delete_option( 'option_name' );
}

Deleting an option only carries one argument, the option_name you give the option. It’s as simple as that, and when the plugin is deactivated, your impression on the database is also removed. Thanks for being one of the good guys in the plugin world!

The full example

Here’s a single view of all the functions I exampled above for easier copy/pasting:

if ( is_admin() ) { //Adds admin verification
register_uninstall_hook('plugin-folder/main-plugin-file.php', 'de_register_settings_something-unique');
register_activation_hook('plugin-folder/main-plugin-file.php', 'pre_register_settings_something-unique');
}

function pre_register_settings_something-unique() { // Upon Activation set defaults
update_option( 'option_name', 'option_value' );
}

function de_register_settings_something-unique() { // Delete options from db on deactivate
delete_option( 'option_name' );
}

{ 2 comments… read them below or add one }

Brian Krogsgard February 3, 2012 at 8:41 am

I highly recommend you only delete options on the register_uninstall_hook rather than deactivate. It is highly annoying to a user when plugin options are removed after just deactivating a plugin, when sometimes it’s just for quick debugging, and not with intent to remove the plugin entirely from the site.

Chris K February 5, 2012 at 9:34 am

@Brian,

This is a good point, I’ll update the post accordingly.

Leave a Comment

Previous post:

Next post: