Saving multiple option values of your WordPress plugins or themes settings into a single database row is quite easy in WordPress thanks to its settings API. The settings API provides everything we need to do so. Here are the real examples taken from my plugin Simple Drop Cap.
First of all, we need to register our WordPress plugins or themes settings with
register_setting() function like this:
The first thing we do is add an action hook to call our register setting function. We use
admin_init hook in order to call the function.
Then we define our callback function which is in it, call
register_setting() function. The
register_setting() function accepts three parameters:
1. Settings group name
This settings group name parameter will be used when we display our setting form on our plugin or theme settings page.
2. Option name
The option name parameter is the name of database row that the settings will be saved into. If our option name is
prfx_options, then the settings will be saved into
prfx_options row inside
3. Sanitization callback function name
The sanitization callback function parameter will call our callback function to sanitize our setting inputs before they’re submitted into the database.
Display Setting Form
The next thing we do is display our plugin or theme setting form. I won’t cover exact steps how to display our setting form since this post is about how to save multiple option values in a single array. I may cover that topic in other post.
To save our multiple option values in a single database row, we need to give our input element type a name that match with our option name parameter that we specified when we register our setting. See the following code:
On line 3, we set the setting group name of the form to name that we already specified in our
register_setting() function. We use
setting_fields() function to do so and it must be put inside the
<form> html tag. The
setting_fields() function will automatically handle nonce, action, and option_page of the form.
Then on line 5, we retrieve the option values from the database using
get_option() function. It accepts one parameter which is our option name.
The next thing we have to pay attention to is input name attribute. This is where the magic happens. For example on the line 13 of the code above. We gave our input a name attribute
prfx_options[option_display_mode]. It says that the input values will be saved into
prfx_options database row with
option_display_mode key. It’s much like php array but without the quotes on the key.
To retrieve the option value, you just need to call it using the variable that we defined on line 5 of the code above. So for example, our variable that contains all the option values is
prfx_options and we want to retrieve the value of
option_display_mode key. We just have to write
$prfx_options['option_display_mode'] to retrieve it. You can add
echo to print the value.
Sanitizing and Saving Option Values
The last step we need to do is sanitizing and saving the option values. After a user enter the option values on the settings page, the values will be sanitized before they’re saved into database. Data validation and sanitization is important to prevent SQL injection or other malicious efforts that try to hack or interfere our website. The sanitization process will be done in our sanitization callback function that we specified as third parameter of
register_setting() function. The function name must match the third parameter name of
register_setting() function. Here are our codes to sanitize the option values:
We need to sanitize every value that a user inputs on our settings page. Every value has its own key so we just need match the key name of the
$input parameter with the name attribute of our input elements on our settings page. Note that you can change
$input variable name with anything you want. It only indicates that it carries the values sent from the settings page form.
On line 2, we sanitize our input element which has
option_display_mode key. On line 3, we sanitize input element which has
option_font_size key, and so on. For sanitization functions available in WordPress you can read about it more here.
That’s it! We can now save all of our option values into a single database row. Let me know if you have any question regarding this topic. I will answer it as best as I can.