钩子文档

switch_blog

💡 云策文档标注

概述

switch_blog 是一个 WordPress 动作钩子,在切换博客时触发,常用于多站点环境。它提供新博客 ID、旧博客 ID 和上下文参数,以支持自定义操作。

关键要点

  • switch_blog 钩子在调用 switch_to_blog() 或 restore_current_blog() 时触发。
  • 参数包括 $new_blog_id(新博客 ID)、$prev_blog_id(旧博客 ID)和 $context(上下文,如 'switch' 或 'restore')。
  • 相关函数有 switch_to_blog() 和 restore_current_blog(),用于切换和恢复当前博客。
  • 自 WordPress 5.4.0 版本引入,适用于多站点(MU)环境。

📄 原文内容

Fires when the blog is switched.

Parameters

$new_blog_idint
New blog ID.
$prev_blog_idint
Previous blog ID.
$contextstring
Additional context. Accepts 'switch' when called from switch_to_blog() or 'restore' when called from restore_current_blog() .

Source

do_action( 'switch_blog', $new_blog_id, $prev_blog_id, 'switch' );

Changelog

Version Description
MU (3.0.0) MU (3.0.0)
5.4.0 Introduced.