WordPress.org

Forums

How do I limit admin_head content to certain pages? (8 posts)

  1. Glenn Ansley
    Member
    Posted 7 years ago #

    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?

  2. nickohrn
    Member
    Posted 7 years ago #

    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.

  3. Glenn Ansley
    Member
    Posted 7 years ago #

    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.

  4. Glenn Ansley
    Member
    Posted 6 years ago #

    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.

  5. Glenn Ansley
    Member
    Posted 6 years ago #

    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.

  6. askapache
    Member
    Posted 6 years ago #

    <?php
    
    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!

  7. Glenn Ansley
    Member
    Posted 6 years ago #

    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.)

  8. yoshi
    Member
    Posted 6 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic