Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What Wordpress hook fires first admin_init or admin_menu

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'));
like image 875
Daniel Huckson Avatar asked Nov 25 '14 22:11

Daniel Huckson


People also ask

Which hook fires after entire WordPress site has finished loading?

do_action( 'wp_loaded' ) This hook is fired once WP, all plugins, and the theme are fully loaded and instantiated.

What is Admin_init in WordPress?

admin_init is triggered before any other hook when a user accesses the admin area. This hook doesn't provide any parameters, so it can only be used to callback a specified function.

How do I add a menu to the admin dashboard in WordPress?

Creating menu –add_action('admin_menu', 'custom_menu'); add_action('admin_menu', 'custom_menu'); In above line of code, first parameter is the hook we discuss about, Second parameter is name of callback function. In callback function you have to write what you want to alter in admin menu.


1 Answers

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
like image 178
diggy Avatar answered Sep 23 '22 03:09

diggy