与相关this answer, 哪些国家
每次switch_to_blog()
你need 调用restore_current_blog()
否则,WP会认为它处于“切换”模式,并可能返回不正确的数据。
我已经做了一些测试,可以确认这是一个问题。(它还回答了我在工作中遇到的一个关于文件上传URL的问题,对此我竖起大拇指。)
在我的测试(详情如下)中,我发现有两种方法可以继续:
始终配对switch_to_blog()
具有restore_current_blog()
switch_to_blog(), 最后一个切换回你开始的博客unset( $GLOBALS[\'_wp_switched_stack\'] );
在最后
我的测试将以下代码添加到functions.php
在我的一个WordPress多站点安装上:
add_action( \'shutdown\', \'pj_stb_test\' );
function pj_stb_test() {
if( ! current_user_can( \'update_core\' ) )
return;
$home = get_current_blog_id();
$checklist = array(
\'constants\' => array( \'UPLOADS\', \'MULTISITE\', \'BLOGUPLOADDIR\' ),
\'functions\' => array( \'ms_is_switched\', \'wp_upload_dir\' ),
);
$site_ids = array( 1, 2, 3 );
echo( "switch_to_blog() chain:<br />" );
foreach( $site_ids as $id ) {
switch_to_blog( $id );
}
switch_to_blog( $home );
_pj_dump( $checklist );
echo( \'switch_to_blog() chain followed by
unset( $GLOBALS[\\\'_wp_switched_stack\\\'] )<br />\' );
foreach( $site_ids as $id ) {
switch_to_blog( $id );
}
switch_to_blog( $home );
unset( $GLOBALS[\'_wp_switched_stack\'] );
_pj_dump( $checklist );
echo( \'switch_to_blog() / restore_current_blog() pairings<br />\' );
foreach( $site_ids as $id ) {
switch_to_blog( $id );
restore_current_blog();
}
_pj_dump( $checklist );
function _pj_dump( $checklist ) {
$constants = $checklist[\'constants\'];
$functions = $checklist[\'functions\'];
echo( "<p>Constants:</p>" );
echo( "<pre>\\n" );
foreach( $constants as $c ) {
echo( $c . \': \' );
var_dump( constant( $c ) );
echo( "\\n" );
}
foreach( $functions as $f ) {
echo( $f . \': \' );
var_dump( call_user_func( $f ) );
echo( "\\n" );
}
}
在我的网络中的任意站点上返回的输出:switch_to_blog() chain:
UPLOADS: string(30) "wp-content/blogs.dir/94/files/"
MULTISITE: bool(true)
BLOGUPLOADDIR: string(50) "/path/to/wp/wp-content/blogs.dir/94/files/"
ms_is_switched: bool(true)
wp_upload_dir: array(6) {
["path"]=>
string(57) "/path/to/wp/wp-content/blogs.dir/94/files/2013/11"
["url"]=>
string(74) "http://example.com/my-site/wp-content/blogs.dir/94/files/2013/11"
["subdir"]=>
string(8) "/2013/11"
["basedir"]=>
string(49) "/path/to/wp/wp-content/blogs.dir/94/files"
["baseurl"]=>
string(66) "http://example.com/my-site/wp-content/blogs.dir/94/files"
["error"]=>
bool(false)
}
switch_to_blog() chain followed by unset( $GLOBALS[\'_wp_switched_stack\'] )
UPLOADS: string(30) "wp-content/blogs.dir/94/files/"
MULTISITE: bool(true)
BLOGUPLOADDIR: string(50) "/path/to/wp/wp-content/blogs.dir/94/files/"
ms_is_switched: bool(false)
wp_upload_dir: array(6) {
["path"]=>
string(57) "/path/to/wp/wp-content/blogs.dir/94/files/2013/11"
["url"]=>
string(50) "http://example.com/my-site/files/2013/11"
["subdir"]=>
string(8) "/2013/11"
["basedir"]=>
string(49) "/path/to/wp/wp-content/blogs.dir/94/files"
["baseurl"]=>
string(42) "http://example.com/my-site/files"
["error"]=>
bool(false)
}
switch_to_blog() / restore_current_blog() pairings
UPLOADS: string(30) "wp-content/blogs.dir/94/files/"
MULTISITE: bool(true)
BLOGUPLOADDIR: string(50) "/path/to/wp/wp-content/blogs.dir/94/files/"
ms_is_switched: bool(false)
wp_upload_dir: array(6) {
["path"]=>
string(57) "/path/to/wp/wp-content/blogs.dir/94/files/2013/11"
["url"]=>
string(50) "http://example.com/my-site/files/2013/11"
["subdir"]=>
string(8) "/2013/11"
["basedir"]=>
string(49) "/path/to/wp/wp-content/blogs.dir/94/files"
["baseurl"]=>
string(42) "http://example.com/my-site/files"
["error"]=>
bool(false)
}
很抱歉这么冗长,但我想确定我这里有所有的东西。