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
ptsingelton 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'));
admin_menu
seems to fire beforeadmin_init
, here’s what happens:admin_menu
fires inwp-admin/includes/menu.php
on line 149wp-admin/menu.php
on line 255wp-admin/admin.php
on line 115admin_init
fires in that same file on line 145WordPress 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!