5

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?

1 Answer 1

10

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

Your Answer

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

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