How to Register Plugin Options

I’m currently writing my first WordPress plugin: WP-jPlayer.  I’m taking the jQuery-based audio player jPlayer and managing the options and songs from the Admin pages.  The plugin isn’t finished yet, but I’ve already learned a ton, so I wanted to share the tips and tricks I’ve picked up.  Some were learned from other developer‘s plugins, some by reading articles on the web, and many by simply making mistakes.

WordPress has a wonderful API, which allows you to use WordPress’s built-in functions to accomplish certain tasks.  One of the first API hooks you’ll use is add_action.  Add_action connects your custom function to a WordPress action.  The proper usage of add_action is:

<?php add_action( $tag, $function_to_add, $priority, $accepted_args ); ?>

$tag is the WordPress action you want to hook onto.  This is required when using add_action.

$function_to_add is your custom function to hook onto the WordPress action.  This is required when using add_action.

$priority determines the importance of your function; 10 is the default setting.  If it’s higher priority, use a lower number, if it’s lower priority, use a higher number.  This is optional when using add_action.

$accepted_args sets how many arguments can be passed on to your function.  This is optional when using add_action.

Here’s some example code from my WP-jPlayer plugin:

// this function registers our settings in the db
add_action('admin_init', 'wp_jplayer_register_settings');
function wp_jplayer_register_settings() {

register_setting('wp_jplayer_songs', 'wp_jplayer_songs', 'wp_jplayer_songs_validate');
register_setting('wp_jplayer_options', 'wp_jplayer_options');

}

The first line is a note, documenting what the function does.  Documenting your plugins and themes is vital, especially for updating the plugin later or someone else reading your code.

The second line uses add_action and specifies the WordPress action admin_init will hook to my function wp_jplayer_register_settings.  Admin_init is the first action run when someone accesses the WordPress Admin area.  In this case, it tells WordPress to run my function when the plugin is activated.

The function itself is basic PHP, but the register_setting hook is a WordPress thing.  It registers your plugin settings with WordPress and hooks a function that will sanitize the data so it will be saved in the database properly without any issues.  I’ll do another blog post about sanitizing data.

The proper usage of register_setting is:

<?php register_setting( $option_group, $option_name, $sanitize_callback ); ?>

$option_group is the name of the settings group, which can be anything you want.  This is required when using register_setting.

$option_name is the name of the setting you want to save.  This is required when using register_setting.

$sanitize_callback is the custom function that sanitizes the data.  It is optional, but highly recommended.

As you can see from my example code above, I registered two settings for my plugin: wp_jplayer_songs, the player’s song data, and wp_jplayer_options, the settings for the plugin itself.

You can register as many settings as you want, but it’s advisable that you keep them to a minimum.  Each setting requires a database visit, which slows down your site a bit.

So there you go.  Copy my code above and paste it into the top of your plugin file and customize for your settings.  When the plugin is activated, your settings will be registered with WordPress and you’re ready to use them!