1

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?

2 Answers 2

1

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

<?php
    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.

1
  • 2
    You should only add admin pages on admin_init. Commented Apr 20, 2013 at 11:32
1

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
    'Title',
    'Menu',
    'activate_plugins', // Administrators
    'slug',
    function(){ // Visual is here ?>
        <div class="wrap">
            <h2>Sub-Page Title</h2>
            <!-- Subpage visual output comes here -->
        </div>
<?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!

8
  • 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 :) Commented Oct 19, 2011 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. Commented Oct 19, 2011 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. Commented Oct 19, 2011 at 21:18
  • @kaiser Hostgator (no affiliate link, shared hosting support for PHP 5.3) Commented Oct 19, 2011 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. Commented Oct 19, 2011 at 21:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.