Configuring WordPress Plugins on Plugin Activation

Most of the WordPress sites I build now involve a custom theme and some custom plugins. Often these custom plugins are used to extend the functionality of an existing WordPress plugin. One of the big workflow issues is always taking all the setting of your plugin on the development or local sites and making sure the live site has the same settings.

I used to make a list of the settings while I configured the local version of the site. Then when we launched a new feature, I’d take that list and follow it as I configured the live site. That’s a lot of manual work and it simply is no fun. What I do now is create activation actions inside my custom plugins to set options for whatever I need.

Today we’re going to look at setting an option in Cubepoints in our custom plugins. Start by installing and activating Cubepoints. Then create a custom plugin to extend Cubepoints. It’s the custom plugin for Cubepoints that we are going to be putting our code in.

Plugin Activation Hooks

What we want to do is when our custom plugin runs, set Cubepoints to the defaults that we need for the live/testing site to work as we expect. We do not want to run these options every time the site or WordPress admin runs so we don’t want to use any of the standard WordPress hooks you may be familiar with. We want to use a hook that only runs when we activate the custom plugin. To do this we are going to use the function register_activation_hook.

register_activation_hook

register_activation_hook has 2 arguments.

$file
The path to the main plugin file. I use `__FILE__` here which represents the file the function is in.
$function
The function you want to run when the plugin is activated.

Now lets look at the code we are going to run when we activate the plugin.

<?php
/**
 * Calling all these things on plugin activation only so we don't call them over and over
 *
 * @action  theme_t_wp_cubepoints_extension_activation
 *
 *
 * @since   1.0
 * @author  WP Theme Tutorial, Curtis McHale
 */
function theme_t_wp_points_activation_actions(){

    do_action( 'theme_t_wp_cubepoints_extension_activation' );

}
register_activation_hook( __FILE__, 'theme_t_wp_points_activation_actions' );
?>

As you can see above, I’m actually calling another action inside this function. I’m doing this so that any other plugin that has code to run when this plugin is activated can easily use that action to run it’s code. We could certainly put all the code we want to run on activation right in the plugin, but this is a far more extensible way to do things.

Setting Our Option

Now we are going to set the ‘symbol’ that Cubepoints uses in front of points. Let’s simply set it as blank, so we have no symbol by default. You can typically tell what the option is save as in the database by viewing source on the input in the WordPress admin. So go to Cubepoints/Configure and view source/inspect element on the ‘Prefix for display of points’ option. We can see that the name of the input is cp_prefix. If we were to look in the database we’d also see that in the options table there is a $ saved as the option_value for the option_name of cp_prefix. Knowing that we can now save our option of no value.

To save an option we are going to need to use the update_option function. update_option takes 2 values.

update_option

$option_name
The name of the option in the database.
$new_value
The value you want to save for the option name.

Now lets look at our code.

<?php
/**
 * Sets the 'symbol' that Cubepoints uses before point values
 *
 * @action  theme_t_wp_cubepoints_extension_activation  Runs when we activate the extension plugin for Cubepoints
 *
 * @since   1.0
 * @author  WP Theme Tutorial, Curtis McHale
 */
function theme_t_wp_set_cubepoints_symbol(){
    update_option( 'cp_prefix', ' ' );
}
add_action( 'theme_t_wp_cubepoints_extension_activation', 'theme_t_wp_set_cubepoints_symbol' );
?>

The code is nothing fancy. All we are doing is using update_option to set a blank value for the cp_prefix option. We could certainly do more complex things. For my client we also added a bunch of default users to the ignored users list for top points rankings. We know that the site admin, myself and another developer should never show up in the rankings so instead of remembering to add those names to a list that doesn’t get transferred between environments, we set it on plugin activation. Saving lots of time and the chance at making a mistake.

Screencast

Curtis McHale

Posts Twitter

Curtis is a web designer/developer specialized in WordPress eCommerce development on WooCommerce. You can get in touch with him about projects on SFNdesign.
My Blog is 4 Times Faster Than Your Blog

Trackbacks and Pingbacks:

  1. Organizing WordPress Plugin Code » WP Theme Tutorial - February 19, 2013

    [...] organizing plugins. If you want to step your plugin development to the next level see my post on Configuring WordPress Plugins on Activation. I’ll also be posting in the future on how to make sure another plugin is installed before [...]

Leave a Reply

*

Text formatting is available via select HTML.

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>