Make WordPress Core

Opened 3 years ago

Closed 3 years ago

#21727 closed enhancement (invalid)

add a new action for scripts and style files in the footer

Reported by: alexvorn2 Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: Administration Keywords: needs-patch
Focuses: Cc:


add_action('admin_print_scripts-' . $page, 'my_plugin_admin_scripts');

This action adds a custom script file to the header only, introduce a new action for the footer too.


Change History (4)

#1 @SergeyBiryukov
3 years ago

  • Component changed from Formatting to Administration

#2 @alexvorn2
3 years ago

you should try to test with a wp_enqueue_style() function for the "admin-header-$page" action, and you will see that the style file is shown at the bottom and not in the head.

#3 @SergeyBiryukov
3 years ago

The proper hook for wp_enqueue_style() in the admin header is admin_enqueue_scripts. $hook_suffix is passed as a parameter in this case.

This example includes a custom script in the header of wp-admin/edit.php:

function my_scripts_method( $hook_suffix ) {
	if ( 'edit.php' == $hook_suffix )
		wp_enqueue_script( 'custom-script', get_template_directory_uri() . '/js/custom_script.js', array( 'jquery' ) );
add_action( 'admin_enqueue_scripts', 'my_scripts_method' );

If any of the later actions is used (e.g. admin_head-$hook_suffix), the script is loaded in the footer:

function my_scripts_method() {
	wp_enqueue_script( 'custom-script', get_template_directory_uri() . '/js/custom_script.js', array( 'jquery' ) );
add_action( 'admin_head-edit.php', 'my_scripts_method' );

This is also briefly mentioned in the Examples section of the Codex page you linked to.

#4 @nacin
3 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from new to closed
Note: See TracTickets for help on using tickets.