plugins_loaded
云策文档标注
概述
plugins_loaded 是一个 WordPress 动作钩子,在已激活插件加载完成后触发,用于早期初始化操作,如过滤器设置或插件覆盖。
关键要点
- 触发时机:在插件加载顺序中较早执行,位于 setup_theme、after_setup_theme、init 和 wp_loaded 钩子之前。
- 主要用途:常用于立即设置过滤器或进行插件覆盖,可插拔函数在此点可用。
- 技术细节:通过 do_action('plugins_loaded') 调用,自 WordPress 1.5.0 版本引入。
代码示例
add_action( 'plugins_loaded', array( 'wpdocs_class_name', 'instance' ) );
class wpdocs_class_name {
private function __construct() {
self::init();
}
public static function instance() {
static $instance = null;
if ( is_null( $instance ) ) {
$instance = new class_name;
}
return $instance;
}
public function init() {
wp_die( __( 'Hello World!', 'text-domain' ) );
}
}
原文内容
Fires once activated plugins have loaded.
Description
Pluggable functions are also available at this point in the loading order.
Source
do_action( 'plugins_loaded' );
Changelog
| Version | Description |
|---|---|
| 1.5.0 | Introduced. |
Skip to note 3 content
Steven Lin
Example migrated from Codex:
Skip to note 4 content
DVHOST_CLOUD
you can use instance method when load plugin
add_action( 'plugins_loaded', array( 'wpdocs_class_name', 'instance' ) ); class wpdocs_class_name { private function __construct() { self::init(); } public static function instance() { static $instance = null; if ( is_null( $instance ) ) { $instance = new class_name; } return $instance; } public function init() { wp_die( __( 'Hello World!', 'text-domain' ) ); } }