+ Reply to Thread
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 14

Thread: wp_enqueue_script and login_head

  1. #1
    epicalex's Avatar
    epicalex is offline Hello World
    Join Date
    May 2009
    Location
    London, Berlin, Paris
    Posts
    41

    Default wp_enqueue_script and login_head

    I'm trying to add jQuery to the login head using wp_enqueue_script in a new plugin of mine, and nothing is being output.

    If I hard code and echo the script, still hooking into login_head, then the script is there, but I want to use wp_enqueue_script to keep things neat.

    This is my function:

    Code:
    function ega_jquery() {
    	wp_enqueue_script('ega_ajax', $ega_root . 'ega_ajax.js', array('jquery') );		
    }
    
    and I'm hooking like this (I'm using a class):

    Code:
    add_action('login_head', array(&$epic_grav_ajax, 'ega_jquery'));
    
    Why isn't this working?! Am I using the wrong hook?

    Thanks

  2. #2
    BinaryMoon's Avatar
    BinaryMoon is offline Hello World
    Join Date
    Jun 2009
    Posts
    52

    Default

    Have you tried this?

    add_action('login_head', 'ega_jquery');

    I've never seen the second parameter in add_action sent as an array, I'm not sure why you're doing it that way?

  3. #3
    epicalex's Avatar
    epicalex is offline Hello World
    Join Date
    May 2009
    Location
    London, Berlin, Paris
    Posts
    41

    Default

    @BinaryMoon - Yeah I have tried it like that, but it doesn't seem to make any difference.

    I'm passing the second param like that because all my functions are contained in a class structure. That isn't the problem though, because if I change the ega_jquery function to just echo hello it works.

  4. #4
    dancole's Avatar
    dancole is offline Tavern Regular
    Join Date
    Jan 2009
    Location
    USA
    Posts
    237

    Default

    I'd find a plugin that inserts jQuery can look though that code. For have to include the right things in the right locations... it can be tricky. I'd look through your source code to see what is and isn't happening.

    I'd search in OpenID, it adds jQuery and some script into the login page: http://plugins.trac.wordpress.org/browser/openid/trunk In particular, the login.php file: http://plugins.trac.wordpress.org/br...runk/login.php
    Last edited by dancole; 07-19-2009 at 07:32 PM.
    Dan Cole, Future Engineer.

  5. #5
    epicalex's Avatar
    epicalex is offline Hello World
    Join Date
    May 2009
    Location
    London, Berlin, Paris
    Posts
    41

    Default

    @dancole - This what make much sense to others because of your edit, but as far as I'm aware it's perfectly fine to pass an array in add_action, since it says on add_action any of the syntaxes explained in the PHP documentation for the 'callback' type are valid, with an array being one of them.

    I actually learnt that method from my very first plugin following the devlounge plugin series.

    Anyway. I looked at the plugin you linked to, thanks for that, I hadn't managed to find one that added any script to the login_head; was only looking in the plugins I had installed on my blog! But it leaves me bamboozled. The OpenID plugin just calls wp_enqueue_script inside a function and passes the function to the hook. Simple, as it should be...

    I've just checked through all my variables, made sure all the paths to files are right etc.

    I changed the function so that it both used wp_enqueue_script and an echo at the same time. The echo worked and the enqueue didn't...

    Again, I tried removing the class structure and so not passing an array to add_action, and still nothing...

    I installed the OpenID plugin and the scripts got added to the login_head, so the problem isn't with those functions not working on my particular setup...

    This is annoying.

    EDIT: The page I'm doing this on is http://epicalex.com/wp-login.php?action=register

  6. #6
    BinaryMoon's Avatar
    BinaryMoon is offline Hello World
    Join Date
    Jun 2009
    Posts
    52

    Default

    In that case I am guessing it's where you're adding the script. If login_head is called after "wp_head" (or the equivalent on the admin pages) then it will not be included

    Looking at the openid plugin above the wp_enqueue function is called and then wp_print_scripts is used. This looks like WP doesn't use enqueued scripts so you need to print them yourself.

    so if you add

    wp_print_scripts(array('jquery'));

    does that do the job? (you can see what I am talking about on line 70 here : http://plugins.trac.wordpress.org/br...runk/login.php)

  7. #7
    Otto's Avatar
    Otto is offline Trac Master
    Join Date
    Apr 2009
    Location
    Memphis, TN
    Posts
    770

    Default

    login_head doesn't seem to be hooked up to print the scripts by default. Probably because it doesn't use any scripts normally.

    Given that, change your function to print the script code instead of enqueuing it. Calling wp_print_scripts as suggested by BinaryMoon is the correct way.

  8. #8
    epicalex's Avatar
    epicalex is offline Hello World
    Join Date
    May 2009
    Location
    London, Berlin, Paris
    Posts
    41

    Default

    @BinaryMoon and @Otto - Thank you both for your help on this one, I got both of your methods to work, but went with Otto's because I couldn't work out how to pass a custom script name to wp_print_scripts.

    If you want to see what it was that I was working on, you can go to http://epicalex.com/wp-login.php?action=register . It's just a quick idea I had to get a gravatar using AJAX on the registration page, with an aim to making a user feel more part of the blog's community, ie they are already 'known' by the blog because their avatar is there when they sign up.

    In a future version I'll add header checking so that the message that you see about getting a gravatar only show's if the default displays.

    Let me know what you think, and thanks again all for the help.

  9. #9
    Otto's Avatar
    Otto is offline Trac Master
    Join Date
    Apr 2009
    Location
    Memphis, TN
    Posts
    770

    Default

    To do a custom script, you first register it then call print scripts. wp_register_script takes the same arguments as wp_enqueue_script.

    wp_register_script('ega_ajax', $ega_root . 'ega_ajax.js', array('jquery'));
    wp_print_scripts(array('ega_ajax'));

    I highly recommend this instead of doing your own script tag output, to avoid breaking other scripts doing the same sort of thing to login_head.

    BTW, very cool display of the gravatar. I like it.

  10. #10
    itsananderson's Avatar
    itsananderson is offline Big Tipper
    Join Date
    Jan 2009
    Location
    Terre Haute, IN
    Posts
    354

    Default

    Related to your gravatar display, I noticed that every time the email box loses focus, it updates the gravatar, even if you haven't changed the email field. Is this a bug or a feature?

+ Reply to Thread
Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts