钩子文档

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.

More Information

The hook is generally used for immediate filter setup, or plugin overrides.

The plugins_loaded action hook fires early, and precedes the setup_theme, after_setup_theme, init and wp_loaded action hooks.

Source

do_action( 'plugins_loaded' );

Changelog

Version Description
1.5.0 Introduced.

User Contributed Notes

  1. Skip to note 4 content

    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' ) );
    	}
    	
    }