How can I selectively print scripts to the footer of certain admin pages?

I’m using admin_footer-{$hook_suffix} to selectively print scripts on the new post page and comments page. This hook is depreciated in 3.1.

I see there’s an admin_print_scripts-{$hook_suffix} but this does not print to the footer, rather to the header before any jQuery or other stuff is loaded.

Read More

How can I selectively print scripts to the footer of certain admin pages?

Related posts

Leave a Reply

2 comments

  1. There’s an in_footer parameter that you can pass to wp_enqueue_scripts – does that work?

    I would hook to admin_enqueue_scripts, check the $page for location, and enqueue your script there, with ‘in_footer’ as true.

    Example:

    add_action( 'admin_enqueue_scripts', 'enqueue_my_script' );
    
    function enqueue_my_script( $page ) {
        if ($page !== 'edit.php') return;
        wp_enqueue_script( 'my-script', 'http://path/to/my/local/script', null, null, true );
    }
    
  2. There is also another way to achieve this which allows you to build more dynamic scripts using the admin_footer hook,

        add_action('in_admin_footer', 'my_custom_admin_page');
        function my_custom_admin_page () {  
          //you can check if this is the right page
          $screen = get_current_screen();  
          if('post'== $screen->base && 'my-custom-post' == $screen->id){ 
          ?>
          <script type='text/javascript'>
            jQuery(document).ready( function(){ 
               //check something 
            }); 
          </script>
          <?php 
          }
        }
    

    This method allows you to build a dynamic script to inject into the admin page.