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?
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?
Share Improve this question asked Nov 17, 2020 at 23:40 JonasJonas 1591 silver badge8 bronze badges1 Answer
Reset to default 2If 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.)