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.

Source ref:

do_action('admin_enqueue_scripts', $hook_suffix);
do_action("admin_print_styles-$hook_suffix");

Laying aside the obvious difference in base action ( admin_enqueue_scripts vs admin_print_scripts ), what is the syntactical difference between these two?

I know that for the second, the corresponding add_action() call is like so:

function wpse99999_enqueue_admin_scripts( 'appearance_page_pagename' ) {
    // wp_enqueue_script() call here
}
add_action( 'admin_print_styles-appearance_page_pagename', wpse99999_enqueue_admin_scripts );

But how would the first look? How is the $hook_suffix passed here? Perhaps it is passed as a parameter to the callback?

function wpse99999_enqueue_admin_scripts( 'appearance_page_pagename' ) {
    // wp_enqueue_script() call here
}
add_action( 'admin_enqueue_scripts', 'wpse99999_enqueue_admin_scripts' );

Or is it returned by the callback?

function wpse99999_enqueue_admin_scripts() {
    // wp_enqueue_script() call here
    return 'appearance_page_pagename';
}
add_action( 'admin_enqueue_scripts', 'wpse99999_enqueue_admin_scripts' );

Or is it something else entirely?

share|improve this question

1 Answer 1

up vote 9 down vote accepted

For …

do_action('admin_enqueue_scripts', $hook_suffix);

… the callback gets the hook suffix as first parameter. Since it is an action you don't have to return anything. You can use it in a rather flexible callback function:

add_action('admin_enqueue_scripts', 'wpse_49993_admin_script_callback' );

function wpse_49993_admin_script_callback( $hook_suffix )
{
    switch ( $hook_suffix )
    {
        case 'index.php': // dashboard
        // do one thing
        break;
        case 'options-general.php': // settings
        // do another thing
        break;
    }
}
  • Plus: You can re-use one function on different sites.
  • Minus: The callback will be called on every admin page.

do_action("admin_print_styles-$hook_suffix");

This will be called just on one page.

  • Plus: Slightly faster, smaller risk of collisions.
  • Minus: You need either more add_action() calls or more callback functions to do similar things.
share|improve this answer
1  
Brilliant! Thanks. –  Chip Bennett Apr 23 '12 at 20:21

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.