Difference between do_action(‘admin_enqueue_scripts’, $hook_suffix) and do_action(“admin_print_styles-$hook_suffix”) syntax

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.

Leave a Comment