Support » Plugins » How do I limit admin_head content to certain pages?

  • Hi,
    I am creating a plugin and would like to limit some JS to the write post/page screens only. I am currently using wp_print_scripts and admin_head to add the code but this adds it to *all* admin pages.

    I seem to remember there being a way to limit this to specific pages without using PHP conditionals. Could someone point me in the correct direction?

Viewing 7 replies - 1 through 7 (of 7 total)
  • I believe what you are referring to was if you hooked into wp_print_scripts-<page-hook>. Unfortunately, I haven’t found a way to do that for just the write page and write post screens. Here’s the approach I’ve taken:

    if( strpos( $_SERVER['REQUEST_URI'], 'post-new.php' ) ||
    				strpos( $_SERVER['REQUEST_URI'], 'page-new.php' ) ||
    				strpos( $_SERVER['REQUEST_URI'], 'post.php' ) ||
    				strpos( $_SERVER['REQUEST_URI'], 'page.php' )) {
    				wp_enqueue_script( 'reference', '/wp-content/plugins/plugin-folder/plugin-file.js', array( 'prereqs' ) );

    I know there was someone out there who used some $_GLOBALS[‘editing’] value, but I can’t find it anywhere right now. My approach seems to work, so I’ll stick with it for now.

    Yes, that wp_print_scripts-(page_hook) syntax was what I was looking for.
    I know there was a discussion on wp-hackers earlier this month, but they referred primarily to the plugin option pages. It would be great to know if anybody else has a solution for the write post and page screens. I’ll try your approach for now. Thanks.

    I’m having trouble with the above code not working correctly on some server setups. Does anyone know how the WordPress Core determines this. For instance, the add_meta_box() function asks you to specify what page you want it to show up on. That suggests to me that there is code somewhere that knows what page your on (post-new or page, etc). I can’t seem to find it though.

    Never mind, I see that the respective form templates hardcode their name to the do_meta_boxes() function. Still, if you detect what admin page you are on differently than the above solution, I’d like to know how you do it. Thanks.

    add_action( 'admin_menu', 'aa_pro_setup_options' );
    function aa_pro_setup_options(){
      $page=add_options_page( 'AskApache Pro', 'askapache-pro', 'AA Pro', 'askapache-pro', 8, basename(__FILE__), 'aa_pro_main' );
      add_action( 'admin_head-'. $page, 'aa_pro_admin_header' );
    function aa_pro_admin_header(){
      echo '<p>Only ran when on the plugin page!</p>';

    This works perfectly for the askapache plugins.. took me awhile to find how to do it though.. enjoy!

    Thanks. This looks like it will work great. Since my last post I had started conditionally testing against the $the_current_page variable that is set in the admin header. This solution is much cleaner though. (By the way, this $page= solution wasn’t working for me in one of the 2.5 versions.)

    FYI, if you want to include js files in non-plugin pages, you can check against a global variable $pagenow which holds the filename such as ‘post.php’.

    Plus, load-$pagenow and admin_head-$pagenow are also useful action hooks to add processes to a certain page.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘How do I limit admin_head content to certain pages?’ is closed to new replies.