Taken together, the three functions of Options API enable you to add, update, and delete options in the WordPress database. This tutorial provides examples for each of these functions, which can be useful when you need to manage options outside of the Settings API.
- [Instructor] In this video, we look at how to use the Options API to add, remove, read, and update options. The Options API gives us an easy way to store data in the WordPress database. Here's the Codex guide to the Options API, which we'll summarize in this tutorial. Here are the main functions for working with the Options API. All options handled by these functions are stored in the WordPress database. We can find all WordPress options in the options table.
Here we can find all of the options for our WordPress site. We've already seen some examples of the get_option function in the previous tutorial, Adding Settings Callback Functions. In that video, we used get_option to get the options that were added via the Settings API. So, Options handled by the Settings API and the Options API are all stored in the same Options table in the database. Fortunately, the Options API is much simpler to work with than the Settings API.
Let's take a look. From the Exercise Files for this tutorial we have a plugin that shows how to add, update, and delete options using Options API. You can use these functions in combination with the Settings API or you can use them on their own is totally fine. Let's go through each of these examples. First step, this function shows how to use add_option to add a new option to the Wordpress options table. In this function we include a comment that contains the function definition so we can use it for reference.
So with that in mind, we first to find the value of our new option, then we add the new option which has a name called myplug-option-name. So this first parameter is the name of the option. Notice that we are following best practices and prefixing the option with a unique string. Check out the previous video on Best Practices to learn more about proper name spacing. The second parameter is the value of the option which we define here.
Believe it or not, that's all that's required to add an option to the database, but there are two other parameters to keep in mind. This one is a deprecated parameter that you should always leave blank. And this one determines whether or not the option should be automatically loaded with Wordpress. It's usually fine to just leave these at their default values unless you have reason to do otherwise.
So then, with everything in place we can register our function with the admin_init hook. We've already covered this hook in previous tutorials. Now, in order to check the results of this function we first need to uncomment the action hook to enable the function. And then we can load any page in the Admin area to fire our function via the admin_init hook. We just reloaded the Plugins page so that should do the trick. To see the added option we can visit the database using phpMyAdmin.
The new option is stored in the options table. So we can click search to perform a search. Remember, the name of our option is myplugin-option-name. In the search results, we see that the new option has been added thanks to the add_option function and the admin_init hook. Returning to the code editor we want to disable this function for the next example.
Next, we have the update_option function. Here we are using our newly added option with an array value instead of a string. So we can use either strings or arrays for option values depending on what we need to do. Then we call the update_option function. It accepts three parameters. First is the option name. Next, the option value, which we have here.
And there is also a third parameter that determines whether or not Wordpress should autoload the option. It's fine to leave this at the default value, unless you have good reason to change it. Notice that we can use the admin_init hook for this function just like we did for add_option. So, let's enable this function by uncommenting the action hook. And remember to save changes. And to fire the admin_init hook we can load any admin page. Now that we have reloaded the Plugins page the update_option function has been executed.
Let's visit the database to view the results. Here, we can do a new search. And click go. In the search results we see that the option has been updated with our new array value. See, this is the syntax used by Wordpress to store an array. Returning to the code editor, we want to disable this function for the next example. Last but not least, we have the delete option function. This function deletes our plugin option using the Wordpress function delete_option.
Delete_option accepts only one parameter. This is the name of the option that we want to delete. Again, we can register our function using the admin_init hook. To check the results we enable the function and return to the Admin area to fire the hook. After reloading the Plugins page we can hop over to the options table.
And do another search. And no results, the option has been deleted. Taken together these three functions enable us to add, update, and delete options in the Wordpress database. This tutorial provides examples for each of these functions which can be super useful when you need to manage options outside of the Settings API.
- WordPress APIs
- Action and filter hooks
- Activating and deactivating plugins
- Plugin security
- Creating the directory and files
- Adding menus and the settings page
- Inserting custom functionality
- Testing and debugging WordPress plugins
- Creating widgets
- Managing users and roles
- Adding custom post types and taxonomies
- Working with custom fields and database queries
- Using APIs: Transients, HTTP, and REST