2

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?

1 Answer 1

2

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.