I've read this on codex and seem some themes use it but never understood how it worked.

(string) $hookname used internally to track menu page callbacks for outputting the page inside the global $menu array

I can't seem to understand the codex's explanation. Can some one enlighten me on how can we use the return value and what are the uses of it?


One important use is enqueue script / style only on specific plugin/admin pages.

    add_action('init', 'my_plugin_admin_page');

    function my_plugin_admin_page()
        //create the menu page
        $hook = add_menu_page(....);

        //use the hook for this page for enqueuing
        add_action('admin_print_styles-' . $hook, 'my_plugin_admin_styles');

    function my_plugin_admin_styles()
        //enqueue the style/script here

This is one use I know.

| improve this answer | |
  • 2
    You should only add admin pages on admin_init. – Mamaduka Apr 20 '13 at 11:32

Hope code is self-explanatory.

// Add a new submenu page and also add a load handler for it to process POSTs
($hook_name = add_submenu_page(
    'plugins.php', // Under plugins menu
    'activate_plugins', // Administrators
    function(){ // Visual is here ?>
        <div class="wrap">
            <h2>Sub-Page Title</h2>
            <!-- Subpage visual output comes here -->
<?php })) and (add_action("load-{$hook_name}", function(){ // Hook the load here
    if(!strcasecmp($_SERVER['REQUEST_METHOD'], 'POST')){
        $_POST =  stripslashes_deep($_POST); // Fix this WP BS security joke
        // Handle a POST request
        // ... Do stuff with $_POST
    }elseif(!strcasecmp($_SERVER['REQUEST_METHOD'], 'GET')){
        // Handle a GET (normal) request
        // ... Usually not needed

Have fun!

| improve this answer | |
  • 1
    Note, that anonymous/closure functions are not available for PHP v5.2 and before. As WP requirements is PHP 5.2(.1?), this may not work everywhere. You could do a version_compare() before the statement and in case switch to lambda functions. Anyway, there are much easier ways to do this (including readability & debugging/backtracing). +1 for the effort anyway :) – kaiser Oct 19 '11 at 20:57
  • I know but I can no longer poison the function-space since moving completely to PHP 5.3 :) And it's a lot easier for me to post examples. Those that don't know them... learn something new and might be tempted to update PHP. Those that do can quickly understand and convert the code for backwards compatibility. – EarnestoDev Oct 19 '11 at 21:10
  • I see this with mixed feelings: Shared hosting (which is majority) mostly have no chance to decide about the exact version number of PHP they are running. Also: Try to remove an action later on, when the name starts with "\0lambda_XYZ" or some funky closer alphanumeric chaos :) Normally this means catching all functions attached to a hook, do some slow preg_match or similar and attach what you need again. Lots of effort which is IMHO often not worth it. – kaiser Oct 19 '11 at 21:18
  • @kaiser Hostgator (no affiliate link, shared hosting support for PHP 5.3) – EarnestoDev Oct 19 '11 at 21:24
  • 1
    Ok. • Lambda function = create_function(). • Closure (invokable object, object that implements __invoke (at the core)) = Closure (anonymous function). • Function = regular named PHP function. ••• That guy just proved Closures are faster than (named) declared functions. Seriously! No lambda there, just a misnamed Closure variable. – EarnestoDev Oct 19 '11 at 21:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.