switch_to_blog()
Проверено на WordPress 6.9, обновлено Источник: WordPress Developer Resources.
Сигнатура
switch_to_blog( int $new_blog_id, bool $deprecated = null ): true
Описание
Эта функция полезна, если нужно получить записи или другую информацию из других блогов. Вернуться обратно можно с помощью restore_current_blog() .
PHP-код, загруженный вместе с изначально запрошенным сайтом, например код плагина или темы, не переключается. См. #14941.
См. alsorestore_current_blog()
Оригинал (английский)
This function is useful if you need to pull posts, or other information, from other blogs. You can switch back afterwards using restore_current_blog() .
PHP code loaded with the originally requested site, such as code from a plugin or theme, does not switch. See #14941.
Параметры
$new_blog_id
int
обязательный
$deprecated
bool
необязательный
= null
Возвращаемое значение
true
Исходный код
wp-includes/ms-blogs.php:500
function switch_to_blog( $new_blog_id, $deprecated = null ) {
global $wpdb;
$prev_blog_id = get_current_blog_id();
if ( empty( $new_blog_id ) ) {
$new_blog_id = $prev_blog_id;
}
$GLOBALS['_wp_switched_stack'][] = $prev_blog_id;
/*
* If we're switching to the same blog id that we're on,
* set the right vars, do the associated actions, but skip
* the extra unnecessary work
*/
if ( $new_blog_id === $prev_blog_id ) {
/**
* Fires when the blog is switched.
*
* @since MU (3.0.0)
* @since 5.4.0 The `$context` parameter was added.
*
* @param int $new_blog_id New blog ID.
* @param int $prev_blog_id Previous blog ID.
* @param string $context Additional context. Accepts 'switch' when called from switch_to_blog()
* or 'restore' when called from restore_current_blog().
*/
do_action( 'switch_blog', $new_blog_id, $prev_blog_id, 'switch' );
$GLOBALS['switched'] = true;
return true;
}
$wpdb->set_blog_id( $new_blog_id );
$GLOBALS['table_prefix'] = $wpdb->get_blog_prefix();
$GLOBALS['blog_id'] = $new_blog_id;
wp_cache_switch_to_blog( $new_blog_id );
/** This filter is documented in wp-includes/ms-blogs.php */
do_action( 'switch_blog', $new_blog_id, $prev_blog_id, 'switch' );
$GLOBALS['switched'] = true;
return true;
}
История изменений
| Версия | Описание |
|---|---|
| MU (3.0.0) | Introduced. |

