钩子文档

activated_plugin

💡 云策文档标注

概述

activated_plugin 是一个 WordPress 动作钩子,在插件激活后触发,用于执行相关代码。注意,如果插件是静默激活(例如在更新过程中),此钩子不会触发。

关键要点

  • 钩子名称:activated_plugin
  • 触发时机:插件激活后,但静默激活时除外
  • 参数:$plugin(插件文件相对于 plugins 目录的路径字符串),$network_wide(布尔值,仅在多站点中有效,指示是否网络范围激活,默认为 false)
  • 相关函数:activate_plugin(),用于尝试在“沙盒”中激活插件并在成功时重定向
  • 引入版本:WordPress 2.9.0

代码示例

function wpdocs_detect_plugin_activation( $plugin, $network_activation ) {
    // do stuff
}
add_action( 'activated_plugin', 'wpdocs_detect_plugin_activation', 10, 2 );

注意事项

  • 如果需要在插件激活时运行代码,建议优先使用 register_activation_hook() 函数,而不是依赖 activated_plugin 钩子。

📄 原文内容

Fires after a plugin has been activated.

Description

If a plugin is silently activated (such as during an update), this hook does not fire.

Parameters

$pluginstring
Path to the plugin file relative to the plugins directory.
$network_widebool
Whether to enable the plugin for all sites in the network or just the current site. Multisite only. Default false.

Source

do_action( 'activated_plugin', $plugin, $network_wide );

Changelog

Version Description
2.9.0 Introduced.

User Contributed Notes