钩子文档

activate_{$plugin}

💡 云策文档标注

概述

activate_{$plugin} 是 WordPress 中用于在特定插件激活时触发的 Hook。它由 register_activation_hook() 内部使用,动态部分 $plugin 指插件的基本名称。

关键要点

  • 此 Hook 在插件激活时触发,但静默激活(如更新期间)不会触发。
  • 参数 $network_wide 仅用于多站点,控制插件是否网络范围激活,默认为 false。
  • Hook 本身不提供参数,函数应输出到浏览器或执行后台任务,不应返回或接受参数。
  • 推荐使用 register_activation_hook() 而非直接使用此 Hook。

注意事项

  • 确保在插件激活逻辑中正确处理网络激活场景。
  • 避免在 Hook 函数中执行长时间阻塞操作,以免影响用户体验。

📄 原文内容

Fires as a specific plugin is being activated.

Description

This hook is the “activation” hook used internally by register_activation_hook() .
The dynamic portion of the hook name, $plugin, refers to the plugin basename.

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

Parameters

$network_widebool
Whether to enable the plugin for all sites in the network or just the current site. Multisite only. Default false.

More Information

This hook provides no parameters. You use this hook by having your function echo output to the browser, or by having it perform background tasks. Your functions shouldn’t return, and shouldn’t take any parameters.

It is recommended to use the function register_activation_hook() instead of this function.

Source

do_action( "activate_{$plugin}", $network_wide );

Changelog

Version Description
2.0.0 Introduced.