Friday , 17 November 2017
Home / Programming / WordPress / Plugins / WordPress Options API – Part 2 – WordPress Plugin Tutorial
WordPress Options API
WordPress Options API

WordPress Options API – Part 2 – 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.

This is part 2 of our WordPress Options API tutorial. We’ll be discussing how to make our plugin more efficient using arrays in a single option setting instead of creating multiple options for 1 plugin.

We’ll be using the same code we used in our last tutorial. Let’s update our file to this.

 WordPress Options API

A couple of things to take in here. We’re not using any new functions here. Instead, we’re creating only 1 option. This 1 option will store our value of the youtube username and the version of the plugin. If a user installed our plugin for the first time, we create an array with the key as the option name and the value for the option value.  We then use the add_option() function to and passed in our array. WordPress takes care of figuring it out that it’s an array and works with the data. If we deactivate and reactive our plugin, you’ll see the new option in the options page. We renamed the option to jk_op_array. You’ll also notice that instead of seeing the values, you’ll see SERIALIZED DATA. 

This means the data for the option is VERY sensitive and you should not change it manually. Instead you should change it using PHP. Let’s move onto the next part. If the user already has the plugin options, we then grab our data using the get_option() function. Since our data is an array, WordPress returns the data in array form for us.

We then check the version and if the version isn’t the latest version we then update that value and then use the update_option() function. We pass in the same array and WordPress once again takes cares of the rest. ?

Array Option vs Multiple Options

By default, WordPress sets multiple options. Your user may end up using other plugins which set up even more options. Their blog will be filled with tons of options. What if your plugin has 30 options? It isn’t wise and not efficient to store 30 different options. Instead, you can have 1 option that’ll contain an array of values.  It is highly recommended you go the array route if you have the need to set multiple options. It’s cleaner, efficient, and your plugin will run faster. Also, it’ll be easier to access your options. You won’t have to use the get_option() function 30 times in 1 file. Another upside is you don’t have to worry about name conflicts. The only name that should be unique is the name of the actual options. Any options INSIDE the array can be generic and simple.

More information about the WordPress Options API can be found here. Your option should appear like this in the options page.

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