Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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 ?

UPDATE

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'); ?>
        </a>

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;
    //fb($hookname);

}
share|improve this question
    
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
add comment

1 Answer

up vote 3 down vote accepted

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') )
        return;
    ?>
    <div class="error"><p><?php echo $hook_suffix; ?></p></div>
    <?php 
}

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.

Addition
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.

example.com/wp-admin/admin.php?page=my-quiz/lib/admin/manage_questions

We can defeat the cap check by querying for..

example.com/wp-admin/admin.php?page=my-quiz/lib/admin/manage_questions.php

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

example.com/wp-admin/admin.php?page=my-quiz/lib/admin/manage_questions.php

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

example.com/wp-admin/admin.php?page=my-quiz/lib/admin/manage_questions

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).

share|improve this answer
    
thanks - I updated the question with more detail. –  codecowboy Feb 11 '11 at 17:31
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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