Dynamic Footer Action in WordPress 4.6

Last updated on by admin

The “admin_print_footer_scripts” action hook is the last chance to localize admin scripts; but it is a generic one. If you ever wanted to do something for specific admin pages only, you would either have to hook into another dynamic action, or hook into “admin_print_footer_scripts” and check the $hook_suffix (which is not even passed) yourself.

The problem with the former is that there might be happening a lot between the chosen action and when your script gets enqueued (admin_print_footer_scripts); and maybe you need to be aware of what has happened. The problem with the latter is that you would have to register possibly a lot of functions, maybe check the current admin page inside several of these, and eventually bail most of the times. That’s why WordPress 4.6 brings the dynamic footer action “admin_print_footer_scripts-$hook_suffix”.

The following is the pre-4.6 code:


add_action( 'admin_print_footer_scripts', function() {

    global $hook_suffix;

    if ( 'some_admin_page' !== $hook_suffix ) {
        return;
    }
    // Whatever it is that you want to do...
} );

It can now be simplified like this:


add_action( 'admin_print_footer_scripts-some_admin_page', function() {
    // Whatever it is that you want to do...
} );

This change brings more consistency between “wp-admin/admin-footer.php” and “wp-admin/admin-header.php”, which already fires the generic “admin_print_scripts” and the dynamic “admin_print_scripts-$hook_suffix” actions.

Leave a Reply

Your email address will not be published. Required fields are marked *