Introduction to using WordPress Options, part 1

By elia | Published: August 21, 2012

This article is a quick introduction tutorial to the WordPress Options plugin and how to use its APIs from your own products, regardless of them being either themes or plugins. In a hurry? Jump straight to the code snippet!

Using the plugin is extremely easy and only requires three steps:

Embedding the plugin files into your own product (optional)

Creating your options array with all of your settings

Calling into the plugin’s API to register your settings

So let’s now look into each one of the steps and how to complete them.

Embedding the plugin

This step is not necessary but highly recommended as it allows for options to work out of the box without forcing your users to install an extra plugin. All you have to do is to download the plugin itself and extract it into your main product’s folder.

At this point all you have to do is simply include the file synved-options/synved-wp-options.php from either the functions.php in your theme or the main file within your plugin. Don’t worry about checking if the plugin was already loaded or included. Just include synved-options/synved-wp-options.php, everything else will be handled automatically by the plugin!

Creating the options

This step is very easy. Just create an array that contains all of your options. The framework is quite flexible in that the amount of required parameters to get a functional options page is very minimal and the array will just become as complex as you want it to be, depending on your needs.

A simple example of a minimal options array that creates 3 options is the following:

As you can see the format for creating options is very simple and clear. All you do is add an extra element in the array for every option you want to create, together with info on the type of the option as well as a label that describes the option and a tip that gives further information.

Note: this is just the absolute simplest example snippet that you can get but you can go much more complex than this and specify many more parameters as well as creating nested options sections and place them in pages anywhere you want in the WordPress admin menu.

Registering the options

This last step is absolutely the simplest in the whole process. All you have to do is to call a specific API function synved_option_register and pass in a unique ID as well as the options array created in the above step. Like this:

synved_option_register('my_test', $test_options);

Once you make this function call you will see a new page under the WordPress Settings tab, called “My Test” and when you open the page you will see something very similar to when you open a built-in WordPress settings page, with an options section called “General Settings”.

All of this, including page name and title as well as section name can be customized easily by slightly adjusting the options array. More on that on a future tutorial. Alternatively you can download our free theme Stripefolio and look into its functions.php file for a more complete example.

Getting the options

Retrieving the values for the options is now very easy. All you have to do is call the API function synved_option_get with the id you used to register the options in the call to synved_option_register and the name of the option. For instance to retrieve the value for the background_image option above you would use this code:

20 Comments

Just ran across this site and your WordPress Options might just be what I need. I need an Option Block on the Page Edit page that will have a list of about 100 URL sites that can be chosen to appear as links on that page. Also, there would be a way to set the order of those items chosen.

If you can let me know if I’m in the ball park by looking at your plugin – it would be much appreciated.

Unfortunately at the moment the plugin only allows adding options to settings pages in WordPress and not on posts/pages but it is a planned feature and high on the priority list so it will likely be added to the next release as soon as there is the time.

I realize the documentation on this plugin is still scarce. What you can do is download our other plugin WordPress Shortcodes or the Stripefolio theme and search for “synved_option_” in their files and you can get a better idea on the available APIs for the plugin itself.

Thanks for you help on categories. I figured out the problem the categories were all empty at the time, so they didnt appear in your dropdown until I added some new posts… I think it should probably always show all the categories, so I leave it up to you to decide if its a bug.. :)

Thanks for your help, I am excited to see the documentation, this is a very good module, and I am looking forward to helping you!

There’s quite a bit of information there on many aspects of the framework. I’m not sure if the package for the WordPress Options plugin is up to date to match that tutorial. I will update it as soon as I have the time next week.

In case things in the examples don’t work you download the WordPress Shortcodes plugin and simply copy and paste the synved-option folder from there to the WordPress Options plugin main folder and replace any files.

No WordPress doesn’t show tooltips but uses the grey description span tag, which is why the plugin shows the tip that way.

My intention for the future of the plugin is to add “context” so you can attach the list of options to any data, like posts. I’d also like to allow people to add their own custom option types so they can display all sort of options,, including the ones you mention. The only problem right now is lack of time :\