Performance of several get_option() calls

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?

Topic plugin-options settings-api plugin-development options Wordpress

Category Web


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.)

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.