admin管理员组文章数量:1315061
I'm trying to understand the difference between get_site_option() and get_blog_option().
Are blog and site two different things? Apologies if this question seems basic, but when referring to a WordPress website, I've always used both terms very loosely (to mean the same thing). I'm now wondering if there is a difference?
I'm trying to understand the difference between get_site_option() and get_blog_option().
Are blog and site two different things? Apologies if this question seems basic, but when referring to a WordPress website, I've always used both terms very loosely (to mean the same thing). I'm now wondering if there is a difference?
Share Improve this question asked Mar 14, 2015 at 23:52 henrywrighthenrywright 3,1076 gold badges39 silver badges65 bronze badges3 Answers
Reset to default 21get_option()
returns an option for the current blog.
In single site installation, the current blog is the only blog. So get get_option()
returns the option for it.
get_site_option()
is used to retrieve an option network-wide. It means that you can get the same option from any site of the network.
When this function is used in single installation, it normally returns the same thing of get_option()
. The value may change because get_site_option()
trigger filter hooks that are not triggered by get_option()
.
Note that once the $wpdb->options
table is blog-specific, network-wide options are stored in the $wpdb->sitemeta
table, that is specific of multisite installations.
get_blog_option()
is the only among the three functions that doesn't receive the option name as 1st argument, but its 1st argument is $blog_id
.
In fact, it is used in multisite installations to retrieve an option from a specific blog whose the id is known.
What this function does is:
switch_to_blog( $blog_id );
$value = get_option( $option, $default );
restore_current_blog();
return $value;
If $blog_id
is the same of current blog id, WordPress just skips the switch_to_blog
part and just calls get_option()
.
This function is defined in the file wp-includes/ms-blogs.php
that is loaded only for multisite installation, so get_blog_option()
is not defined in single site installations.
get_site_option()
- Gets a network wide option. This option is usually added in the Network Admin Settings section of a multisite set-up. If I had 50 sites, it would be a pain to go to 50 different sites and set the same option value. Instead I could set the option value once and have it apply across the network for all sites. See http://codex.wordpress/Function_Reference/get_site_option
get_blog_option()
- Lets you get the value of an option for a specific site. One example might be to get the value of a user specific option for each site. So I could get all of the sites that the user belongs too, loop over the list of site IDs, and use get_blog_option()
passing the blog_id
and option name and get back the result. It's a convenience function that pretty much does the following:
switch_to_blog( $id );
$value = get_option( $option_name );
restore_current_blog();
See http://codex.wordpress/Function_Reference/get_blog_option
tl;dr: get_site_option()
gets a network wide value, get_blog_option()
gets a specific value for a given site without needing to switch to that site first on your own.
Although get_option()
may retrieve the blog option on network sites, sometimes the network filters override the site option.
For example: users_can_register
for the site will always be overridden by the network setting.
To retrieve the site option, sometimes you'll need to remove any network filters which are located in the ms-default-filters.php file in the WP Core.
remove_filter( 'option_users_can_register', 'users_can_register_signup_filter' ); // ignore network override
$registration = get_option( 'users_can_register');
本文标签: multisiteWhat39s the difference between getsiteoption and getblogoption
版权声明:本文标题:multisite - What's the difference between get_site_option and get_blog_option? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741971198a2407847.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论