什么Wordpress钩子首先触发admin_init或admin_menu

时间:2014-11-25 22:20:57

标签: php wordpress plugins activation

根据我在互联网上阅读的内容,admin_init在admin_menu之前触发,这是真的吗?

我已经制作了一个插件模板来测试这个并发现它并非如此。

测试代码如下。我在激活插件时得到的输出是......

2

pt\singelton Object
(
    [page] => pt
    [page_title] => Page Title
    [menu_title] => Menu Title
    [capability] => manage_options
)

注意上面输出中的数字2,它是从admin_menu()函数输出的。

/*
  License: GPL
  Version: 0.01
  Plugin Name: Plugin Template.
  Plugin URI: http://www.dyncomp.net/plugins/plugin-template/
  Description: Plugin Template.
  Author: Dan Huckson
  Author URI: http://www.dyncomp.net
  Text Domain: pt
  Domain Path: /lang/
 */

namespace pt;

class obj {
    private $instance;

    // Activate
    static function activate() {
        $page = __NAMESPACE__;

        if (!($opt = get_option($page)))
            add_option($page, (object) array('page' => $page, 'in_date' => getdate()));
        else if (!isset ($opt->page) || $opt->page !== $page) 
            wp_die('Error: Option ('.$page.') already exsits in database, the plugin can not be activated.');
    }

    // Setup
    function __construct(&$instance) {
        $this->instance = $instance;
    }

    function admin_init() {
        wp_die('1</br><pre>'.print_r($this->instance, TRUE).'</pre>');
    }

    function admin_menu() {
        wp_die('2<br><pre>'.print_r($this->instance, TRUE).'</pre>',2);
    }
}

class singelton {
    static private $instance;

    public static function getInstance($args) {
        $page = $args['page'];
        if (!isset(self::$instance->$page)) {
            self::$instance->$page = new static();
            self::$instance->$page->page = $page;
            self::$instance->$page->page_title = $args['page_title'];
            self::$instance->$page->menu_title = $args['menu_title'];
            self::$instance->$page->capability = $args['capability'];
        }
        return self::$instance->$page;
    }
    private function __clone() {}
    private function __wakeup() {}  
    protected function __construct() {}
} 

$page = __NAMESPACE__;
$instance[$page] = new obj(singelton::getInstance(array(
    'page' => $page, 
    'page_title' => 'Page Title', 
    'menu_title' => 'Menu Title',
    'capability' => 'manage_options',
    'content_icon' =>  'dashicons dashicons-editor-kitchensink'
)));
add_action('admin_init', array($instance[$page], 'admin_init'));
add_action('admin_menu', array($instance[$page], 'admin_menu'));
register_activation_hook( __FILE__, array($instance[$page], 'activate'));

1 个答案:

答案 0 :(得分:10)

admin_menu似乎在 admin_init之前触发了,这里发生了什么:

  • admin_menuline 149
  • wp-admin/includes/menu.php点火
  • 包含在line 255
  • wp-admin/menu.php末尾
  • 包含在line 115
  • 上的wp-admin/admin.php
  • 之后admin_initline 145
  • 上的同一文件中触发