Can the hook admin_print_scripts-$hook_suffix be used for a hook which has been created like $hookname = get_plugin_page_hookname("my-quiz/lib/admin/$code_page", '' );?

.. where $code_page is somefilename.php.

I've tried it and it doesn't seem to work. I think its probably because of the filepath ?


What gets registered by the above code is 'admin_page_my-quiz/lib/admin/question_form' but this only works in a page if you do:

<a href="<?php echo admin_url('admin.php?page=my-quiz/lib/admin/question_form.php&question=edit&question='.$question->ID); ?>" class='edit'>
        <?php _e('Edit', 'my-locale'); ?>

I have to add the .php suffix for that link to work or I get the 'you do not have permission' error.

@t31os function outputs 'nmsi-quiz/lib/admin/question_form.php' and so this doesn't match 'admin_page_my-quiz/lib/admin/question_form'

this works - add_action( 'admin_print_scripts-'.'my-quiz/lib/admin/question_form.php', array($this,'enqueue_my_js'));

Requested code:

$plugin_scripts = array('question_form.php', 'manage_questions.php');

foreach($plugin_scripts as $code_page) {
    $hookname = get_plugin_page_hookname("my-quiz/lib/admin/$code_page", '' );
    $_registered_pages[$hookname] = true;

  • Can i see the code you're using to register the plugin pages please, ie. the add_menu_page and add_submenu_page (or equivalent) calls.. – t31os Feb 12 '11 at 9:05
  • Added. I realise that this is a non-standard way of doing things and that I am not using add_menu_page(). That's because I don't want these pages in the menu. I am aware of the issues surrounding this. wordpress.stackexchange.com/questions/7812/… – codecowboy Feb 12 '11 at 11:56
  • The thing is, you can't have registered pages that don't exist in the menu, either way you'll lose some functionality if you just hack pages into the appropriate arrays, because core code does have expectations on registered pages to exist to some degree in both the $menu and $submenu arrays.. Might i suggest taking a look at the add_menu_page and add_submenu_page functions to see what they do with the incoming data(this will give you an idea of WordPress does with pages registered using these functions - ie. what arrays it adds to, etc..) – t31os Feb 14 '11 at 15:03
  • I've added some sample code and further comments to my answer. – t31os Feb 14 '11 at 16:22

I think it's really a question of whether that code is giving the correct hook name for the given page.

Here's a simple function you can use to output the hook suffix on each admin page, it will appear inside a red error box(so it'll be easy to spot) for admins only.

add_action( 'admin_notices', 'print_admin_pagehook' );
function print_admin_pagehook() {
    global $hook_suffix;
    if( !current_user_can( 'manage_options') )
    <div class="error"><p><?php echo $hook_suffix; ?></p></div>

Load up the page you're having a problem with and compare the value you see in the box with what you're getting back from the code you posted.

Following on from my last comment, you can actually do something like this..

add_action( 'admin_menu', 'testing_registered_pages', 100 );
function testing_registered_pages() {
    global $_registered_pages, $submenu;
    $plugin_scripts = array( 
        'Question Form' => array( 'page' => 'question_form', 'callback' => 'my_callback_1' ), 
        'Manage Questions' => array( 'page' => 'manage_questions', 'callback' => 'my_callback_2' ) 
    foreach( $plugin_scripts as $title => $my_pages ) {
        $hookname = get_plugin_page_hookname("my-quiz/lib/admin/$my_pages[page]", 'my-quiz' );
        $_registered_pages[$hookname] = true;
        $submenu['my-quiz'][] = array( $title, 'manage_options', "my-quiz/lib/admin/$my_pages[page]", $title );
        add_action( $hookname, $my_pages['callback'] );

// Remove the add_action to reference actual files, leave in place to use a callback function

...there's just one problem with this approach, and that's in being able to defeat the capability requirements of the page(s).

Take this URL.


We can defeat the cap check by querying for..


If we switch the code from earlier to use the file extension instead, eg..

    $plugin_scripts = array( 
        'Question Form' => array( 'page' => 'question_form.php', 'callback' => 'my_callback_1' ), 
        'Manage Questions' => array( 'page' => 'manage_questions.php', 'callback' => 'my_callback_2' ) 

..and optionally disable callbacks, ie. use a real file..

// add_action( $hookname, $my_pages['callback'] ); 

..this then gives us..


..it's still possible to defeat capability requirements using..


Regardless of whether you use an actual file or a callback function, in either case the capability requirements(as above) can be circumvented(obviously this would not be desired behaviour).

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.