What Wordpress hook fires first admin_init or admin_menu

7.7k views Asked by At

From what I've read on the internet the admin_init fires before admin_menu, is this true?

I have made a plugin template to test this and find it not to be the case.

The test code is below. The output I get when activating the plugin is ...

2

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

Notice the number 2 in the output above, it's output from the function 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'));
2

There are 2 answers

4
diggy On BEST ANSWER

admin_menu seems to fire before admin_init, here's what happens:

  • admin_menu fires in wp-admin/includes/menu.php on line 149
  • which is included at the end of wp-admin/menu.php on line 255
  • which is included in wp-admin/admin.php on line 115
  • after which admin_init fires in that same file on line 145
0
Rafiq On

WordPress itself updates its ecosystem of hooks over time. Better to use this plugin Another Show Hooks which provides you the real-time hook firing sequence that you can find by yourself!

enter image description here