Monday , 25 September 2017
Home / Programming / WordPress / Plugins / WordPress Options API – Part 1 – WordPress Plugin Tutorial
WordPress Options API
WordPress Options API

WordPress Options API – Part 1 – WordPress Plugin Tutorial

This post is part of the WordPress Plugin tutorial series. To find a link to all tutorials in order, then please click here.

We learned how to create menus. Now, we’ll learn how to use the WordPress options API to allow us to store user settings for our plugin. The options API is a great way to store settings for your plugin. Your users won’t have to define every single option every time they have to use your plugin. They could also customize it whenever they want and you even don’t have to create a database.

WordPress Options API

Your plugin may be simple and creating a table in the database is overkill if you only have a couple of options. You can view all options that are created by going to this link http://yoursite.com/wp-admin/options.php. This page isn’t accessible through any link on your WordPress site. Mainly because you shouldn’t change any of these options. WordPress creates this page just in case you really have to update an option.  As you can see, there are tons of options. Some of them are created by plugins you may have installed. Even themes can create options.

Setting up our Plugin

Let’s create a new plugin named wpOptionsEx. In your main index.php file, add this bit of code four our WordPress Options API plugin.

I changed the name of the plugin activation function to jk_op_setup_options and updated the function for our plugin. As usual, our function checks to see if the WordPress installation has the latest version. From there, we start adding options.

In our if statement, we use the get_option() function to grab an option.  You pass it a string which will represent the name of the option. If it finds the option, it’ll return it’s value, otherwise it’ll return false if the option doesn’t exist. In our case, this is our first time activating the plugin. So, it’ll return false and then we run the code inside the statement. Inside our statement, we use the add_option() function to add the option that didn’t exist.

This function, accepts 2 arguments. The name of the option and it’s value. Like anything else, you want to give your option a unique name. The value doesn’t have to be unique though. You can even set an empty value.  In our example, I’m using the WordPress options API to store a Youtube username. Your user may deactivate your plugin and reactivate it again. In this case, you always want to check if the user already defined their own value for your option. You don’t want to overwrite it unless you’r certain you must do so.

Updating The Plugin

In most cases, you may end up creating updates for your plugin. When updates occur, WordPress reactivates your plugin and your activation function runs again. What if you have to update certain options? Let’s update your main file again to this.

When a user first activates our plugin, we also add the version of the plugin. It’s good to add your plugin version in the options table. If a user already has your plugin, but is updating your plugin, you’ll want to update the version option. New users of the plugin, will already be set to the latest version of the plugin. The update_options() function takes in 2 arguments. The first is the name of the option you want to update and the 2nd is the value you want to set it to.

The WordPress options API is a very powerful tool to use for your plugin. You can find more information here. Activate your plugin and then go to your WordPress Options page and find the option you created. It should look something like this.

WordPress Options API
WordPress Options API

About Jasko Koyn

Check Also

WordPress Meta Boxes

WordPress Meta Boxes – WordPress Plugin Tutorial

WordPress meta boxes are used to display certain information or options in a page. It’s …

Leave a Reply

Your email address will not be published. Required fields are marked *

ULTIMATE WORDPRESS DEVELOPMENT COURSE
Want to learn how to create your own custom WordPress plugins and themes? Check out my course!
Get 93% off my course. You'll also learn how to integrate WooCommerce and BuddyPress into your projects. This is a limited time offer
Yes, I would like to get 93% off your course!
No Thanks!
close-link