I am currently working on my first WordPress plugin. To save and output certain settings I use the native Settings API. Now the question arises, how performant are several calls of the get_option() function.
Since I work object-oriented I use a function which returns the options:
private function get_settings() {
$styles = get_option("style_settings");
return $styles;
}
Every time I need one or more values from the options array, I now call this function. Is it better to assign the options in the constructor to a variable or does it not matter for the performance?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
If you check the implementation of get_option() you’ll see that in line 168 it calls wp_cache_get() like so:
$value = wp_cache_get( $option, 'options' );
This means multiple calls to get_option("style_settings") within the same PHP execution will be served from cache. There is no need to re-invent this, as you might break other functionality with it. (There are various filters and hooks in the get_option() call with which plugins may interfere with this. Unless you have specific reasons, you usually want these to be executed.)
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0