post-page

Adding Scripts Properly to WordPress Part 3 – Page Detection

12
responses
by
 
on
May 8th, 2010
in
HOW-TO, WordPress FAQs
heading
heading
heading
12
Responses

 

Comments

  1. Hikari (26 comments.) says:

    There are special actions that are only available in specific admin pages, that’s the right way to identify in which page you are.

    I have a plugin that helps us to identify these special actions. It also lists all conditional tags in a unique place and all hooks with functions hooked to them.

    http://Hikari.ws/hooks-troubleshooter/

    • Ronald huereca (32 comments.) says:

      But, only admin_print_scripts should you add script files. Is there an additional page-detection technique you can use within this hook?

      • Hikari (26 comments.) says:

        You can hook those actions, and inside them add the script. It may be better than hooking on every admin page and testing current page to add the script.

  2. Otto (215 comments.) says:

    Using GET and SERVER is a pretty terrible way to detect what page you’re on. The global $pagenow is what you should be checking. That can be things like edit.php, or options-general.php.. Depending on what page you’re on.

    But really, you should use the proper hooks to add them only to the pages you’re interested in.

    One of the hooks in the admin section is this one:

    do_action(“admin_print_scripts-$hook_suffix”);

    That ‘hook_suffix’ is special. It can be set to a lot of things, but one thing it does get set to for admin pages is the global $pagenow variable.

    So, to enqueue something only on, say, post-new.php, you could do this:

    function eee() {
    wp_enqueue_script(…);
    }
    add_action(‘admin_print_scripts-post-new.php’,’eee’);

    Much simpler and safer than relying on untrustworthy data. Never resort to the Superglobals when WP offer nicer ways to do things.

    • Ronald huereca (32 comments.) says:

      Coolness. I learned something today ;)

      I will update this post when I update my book. Thanks for sharing.

      Can you also do the same type of thing with wp_print_scripts?

      • Otto (215 comments.) says:

        No, that stuff is really only for the admin. Generally you use the is_home and similar for the front end view of things.

    • Ankur Oberoi (4 comments.) says:

      Where can I find more documentation about the possible hook_suffix’s? I wasn’t able to find it in the Codex. I am specifically looking to figure out if I can detect if I am on an Add or Edit page for a custom post type. Any help would be appreciated. Thanks!

    • Ankur Oberoi (4 comments.) says:

      i think i figured it out, or i at least have one solution to the question i was asking. let me know if you might know of a better solution or any holes i might have missed. (in this example my custom post type is called `slider`)

      in functions.php:

      function my_media_upload_scripts() {
      wp_enqueue_script(`my-script`, content_url(`/themes/twentyten/my-script.js`) , array(‘jquery’), ‘0.2’, true);
      }

      add_action(`admin_print_scripts-post.php`, `my_scripts`);

      in my-script.js:

      if (pagenow == ‘slider’) {
      jQuery(document).ready(function() {

      jQuery(‘#titlediv’).css(‘background-color’, ‘red’);

      });
      }

      i am not sure if this pagenow variable is always available or if another plugin I am using made that available for me. does anyone know where it comes from?

      • Ankur Oberoi (4 comments.) says:

        yikes, sorry for the overload, i couldnt edit my post when it was awaiting moderation.

        i meant:

        function my_scripts() {

        int he functions.php file. excuse the typo.

  3. Andrew@BloggingGuide (90 comments.) says:

    This will be handy in the future. Thanks for sharing this.

  4. bubazoo (213 comments.) says:

    so do you mean running a script without activating it in the plugins area? I mean, isn’t a script or plugin activated on all pages as soon as its activated?



Obviously Powered by WordPress. © 2003-2013

page counter
css.php