最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugin development - Performance of several get_option() calls

programmeradmin0浏览0评论

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 badges
Add a comment  | 

1 Answer 1

Reset to default 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.)

发布评论

评论列表(0)

  1. 暂无评论