0

Some of you might know that I've been really struggling with trying to enqueue Javascript to my Wordpress theme and I just can't get it to work. I need to get this done somehow. I will keep this question updated, I will edit and I hope you will work together with me through this step by step! It's been a few months now and I still can't get it to work and I have no clue why!

Step 1:

I want to move all the script calls in my header.php into my function.php. I transfered everything to my function.php: (updated!)

function add_my_javascripts() {
    /* If this is the admin area of WordPress, don't do anything */
    if( is_admin() )
        return;

    /* Register all of our scripts */
    wp_register_script('jquerytools', 'http://cdn.jquerytools.org/1.2.5/all/jquery.tools.min.js', array('jquery'));
    wp_register_script('jquery.form', get_template_directory_uri() . '/js/jquery.form.js', array('jquery'));
    wp_register_script('sprinkle', get_template_directory_uri() . '/js/sprinkle.js', array('jquery', 'jquery-ui-core', 'jquery-ui-tabs'));
    wp_register_script('custom', get_template_directory_uri() . '/js/custom.js', array('jquery'));
    wp_register_script('tooltip', get_template_directory_uri() . '/js/tooltip.js', array('jquerytools'));
    wp_register_script('tooltip', get_template_directory_uri() . '/js/sliding_tabs.js', array('jquerytools'));
    wp_register_script('contact-form', get_template_directory_uri() . '/js/contact-form.js', array('jquery', 'jquery.form'));

    /* Enqueue the ones that need to be enqueued */
    wp_enqueue_script('sprinkle');
    wp_enqueue_script('custom');
    wp_enqueue_script('tooltip');
    wp_enqueue_script('sliding_tabs');
    wp_enqueue_script('contact-form');

}
add_action('wp_enqueue_scripts', 'add_my_javascripts');

Here's my whole function.php just to make sure that there's not some other mistake that keeps interfering with the wp_enqueue or something: http://pastebin.com/mhCNZbFW Also, I entered the code into my PARENT THEME's function.php! I do not use a function.php for my child theme at all.

And I tried to call the action in my header.php: (UPDATE: Currently NO HOOK at all, also no CALL in my header.php!!)

Result: All the scripts are properly loaded (checked with Firebug), but none of them actually works! Only the "custom.js" works (even without the action being called in the header.php).

Why does this not work? Please help me working through this as it's causing so many problems!

Step2: (RESOLVED) Next step will be to provide no.Conflict wrappers to every jQuery JS (which also doesn't work).

What I already tried: - deactivated all plugins (no effect) - created a function.php for my child theme and moved the code above from the parent theme's function.php there (result: error message

Warning: call_user_func_array() [function.call-user-func-array]: First argument is expected to be a valid callback, 'get_my_scripts' was given in /hermes/bosweb25a/b155/ipg.zoomingjapancom/wp-includes/plugin.php on line 405

- tried another theme (TwentyEleven): same issues - I tried to call it like that in the header.php (as that's how it's done in the TwentyEleven theme:

<?php wp_enqueue_script( 'jquery', 'jquerytools', 'jquery-ui-person', 'jqueryform', 'sprinkle', 'custom', 'tooltip' ); wp_head(); ?>

</head>

That also doesn't work ...

LATEST NEWS: See updated function.php code above. I got to work quite a few things now. As for the jquery-form that was needed for the contact.php I just took the direct script call out of the contact.php and put it into its own js file, then wrapped it with a no_Conflict wrapper and now it works just fine! Only thing I'm worried about is whether Wordpress will now load it on every single page although it's only needed on the contact.php??!!

THE ONLY THING THAT IS STILL NOT WORKING is sprinkle.js: Possible reason is that there might be a conflict with the ui-tabs and the jquerytools-ui-tabs. I should do something like this, but don't know where to put it:

<script type="text/javascript" src="jquery-1.6.2.js"></script>
   <script type="text/javascript" src="jquery.tools-1.2.6-dev.js"></script>

   <!-- must do this before including jquery-ui -->
   <script type="text/javascript">
      // change name of jquery tools tabs so that jquery UI doesn't barf
      jQuery.fn.jtTabs = jQuery.fn.tabs;
   </script>

   <script type="text/javascript" src="jquery-ui-1.8.14.js"></script>

If you need any further information, please let me know! Here's my website for reference. I just can't do this on my own. Please give me some advice! Anything might help :) Thanks so much in advance!

4
  • 1
    try to hook into wp_print_scripts action instead see if it works..
    – Sisir
    Commented Sep 18, 2011 at 9:55
  • Hello. Thank you very much for your comment. I tried to google how to do it, but all I found was "This function will not work if it is called from a wp_head or wp_print_scripts actions, as the files need to be enqueued before those actions are run." Hmm. Would I just replace the "wp_enqueue_scripts" by "wp_print_scripts"?
    – japanworm
    Commented Sep 18, 2011 at 10:08
  • 1
    Check sprinkle.js it needs no conflict wrapper
    – Mamaduka
    Commented Sep 18, 2011 at 19:46
  • I changed it back to the no-conflict version $(document).ready(function(){ $('#tabvanilla > ul').tabs({ fx: { height: 'toggle', opacity: 'toggle' } }); Still no change. I thought it's better to keep everything in a no-conflict wrapper anyways? I got to work other scripts, but not sprinkle.js and jquery-form.js (please also see my updated question). Thanks :)
    – japanworm
    Commented Sep 19, 2011 at 1:35

3 Answers 3

1

First Hint: Copy your parent theme (without modifications) and start a new child theme that only contains the style.css file with the comment and a plain functions.php file. You can move stuff later, but this way you can make sure that nothing interrupts. Solve one task after each other in this plain vanilla setup.

Second Hint: Check your dependencies. For some of your scripts you added dependencies inside wp_enqueue_scripts like jquery-ui-person that I can't find in your endless code above. If the script wasn't found, the dependend files won't load.

Third Hint: Dump the following function in your vanilla child themes functions.php file and check the output:

function wpse28817_check_scripts()
{
    echo '<pre>';
    // Prints the array that registers, etc. your scripts. Should tell a little bit about what's happening.
    print_r($GLOBALS['wp_scripts']);
    echo '</pre>';
}
add_action( 'shutdown', 'wpse28817_check_scripts' );

Fourth Hint: Stop doing everything together. Do it step by step. Go to your new vanilla child theme and just add jQuery. If it appears: Care about the next script and so on.

5
  • Hello and thank you very much. I just updated my question, so please have a look at it. I managed to get almost all of it to work (2 scripts still won't work, trying to figure out why right now). If I absoulutely can't do it, I'll try the vanilla version child theme. Thanks for that idea :)
    – japanworm
    Commented Sep 19, 2011 at 1:31
  • 1
    Glad to here that. Ad vanilla child theme) It's not an idea, more like a "you should do it always like this"-concept. Seems that your Q "wp_enqueue doesn't work" is solved. Please ask the rest on SO, as a) you changed the original Q and b) this Q/A is wp related and the open tasks are js/jQuery Questions.
    – kaiser
    Commented Sep 19, 2011 at 1:50
  • Thanks a lot. Well, you are right. The last taks is Wordpress related for me as I don't know how to embed it into Wordpress. I would just copy and paste it as is into my "head" if it was a "normal" website.
    – japanworm
    Commented Sep 19, 2011 at 1:59
  • Oh, and also, the question about whether it was a good idea to put the direct script call out of the contact.php into an external js file was a good idea or not as in whether Wordpress would now call that script on every single page or not - tha tis Wordpress related, too, I think :)
    – japanworm
    Commented Sep 19, 2011 at 2:01
  • N/P. Anyway, it's not wp related just because it's not working and you're using wp.
    – kaiser
    Commented Sep 19, 2011 at 3:46
2

You can try these.

  1. Make a functions.php file for your child theme and paste your code there. The concept of the child theme is that you don't touch the parent theme when customizing.
  2. move the add_action() to the functions.php.
  3. I believe get_template_directory_uri() will point to your parent theme directory not the child theme directory. For getting child theme directory try this get_stylesheet_directory_uri()
  4. If you wish to add any custom script via wp_enqueue_script() you need to register those scripts first.
5
  • Hello again. I created a function.php for my child theme and moved the code to there. After doing so I got the following error message: Warning: call_user_func_array() [function.call-user-func-array]: First argument is expected to be a valid callback, 'get_my_scripts' was given in /hermes/bosweb25a/b155/ipg.zoomingjapancom/wp-includes/plugin.php on line 405 I'm not sure if it's a good idea to create a function.php for the child theme. Also, I have all the necessary js files in my parent theme directory.
    – japanworm
    Commented Sep 18, 2011 at 11:53
  • 2
    @Sisir for getting Child theme directory, WordPress has the similar function get_stylesheet_directory_uri()
    – Mamaduka
    Commented Sep 18, 2011 at 13:21
  • @japanworm If it's not a valid callback, then your function is missing. I guess you forgot to move it too.
    – kaiser
    Commented Sep 18, 2011 at 15:48
  • 1
    @Mamaduka: Thanks updated my answer :) @japanworm: make sure you move your function get_my_scripts() to the child theme funcitons.php file too. And also any script you need to include in child theme you should move them to child theme directory.
    – Sisir
    Commented Sep 18, 2011 at 17:29
  • I got almost everything to work (although I'm still using my parent theme's function.php), so I guess it works like that after all. Still 2 more scripts to go. Please have a look at my updated question. Thanks a lot!
    – japanworm
    Commented Sep 19, 2011 at 1:32
1

The first problem appears to be a simple, PHP-syntax error:

if (!is_admin()) 

wp_deregister_script('jquery');
wp_register_script('jquery', ("http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"), false, '');
wp_enqueue_script('jquery');

wp_enqueue_script('custom', get_template_directory_uri() . '/js/custom.js', array('jquery'));
wp_enqueue_script('jquerytools', get_template_directory_uri() . '/js/jquery.tools.min.js', array('jquery'));
wp_enqueue_script('jquery-ui-person', get_template_directory_uri() . '/js/jquery-ui-personalized-1.5.2.packed.js', array('jquery'));
wp_enqueue_script('jquery-ui-person', get_template_directory_uri() . '/js/jquery.form.js', array('jquery'));
wp_enqueue_script('sprinkle', get_template_directory_uri() . '/js/sprinkle.js', array('jquery', 'jquery-ui-person', 'jquery-ui-tabs'));

You've not wrapped your conditional code in braces: {}, like so:

if (!is_admin()) {

    wp_deregister_script('jquery');
    wp_register_script('jquery', ("http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"), false, '');
    wp_enqueue_script('jquery');

    wp_enqueue_script('custom', get_template_directory_uri() . '/js/custom.js', array('jquery'));
    wp_enqueue_script('jquerytools', get_template_directory_uri() . '/js/jquery.tools.min.js', array('jquery'));
    wp_enqueue_script('jquery-ui-person', get_template_directory_uri() . '/js/jquery-ui-personalized-1.5.2.packed.js', array('jquery'));
    wp_enqueue_script('jquery-ui-person', get_template_directory_uri() . '/js/jquery.form.js', array('jquery'));
    wp_enqueue_script('sprinkle', get_template_directory_uri() . '/js/sprinkle.js', array('jquery', 'jquery-ui-person', 'jquery-ui-tabs'));

}

Second: what version of jquery are you linking from Google? It looks like version 1. If you absolutely must override the core-bundled jQuery version, absolutely use the same (or newer version).

2
  • Hello. I just updated my question. I got rid of the "if is admin" for now. Does Wordpress always use the newest jquery version, though? Anyways, I still didn't get it to work. Also added some new things that I tried, but didn't work.
    – japanworm
    Commented Sep 18, 2011 at 12:52
  • 1
    @Chip: Google, when using /1/ in url: ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js always returns newest jquery version. Currently: 1.6.4 Commented Sep 18, 2011 at 13:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.