2

I have tried to include my javascript file in the following way:

function theme_scripts()
{

    wp_enqueue_scripts( 'custom-script', get_template_directory_uri() . '/js/menu-fix.js',array('jQuery'), true);
}

add_action('init', 'theme_scripts');

This file is included in functions.php

But for some reason the script does not get included.

I have got wp_head() in header.php and wp_footer() in footer.php.

Edit

So I changed my script to:

    function theme_scripts()
    {
    wp_enqueue_scripts( 'custom-script', get_template_directory_uri() . '/js/menu-fix.js',array('jquery'), '1.0',true);
    }
add_action('wp_enqueue_scripts', 'theme_scripts');

But still it isn't loaded.

This is how my footer.php looks like:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
   <script src="<?php bloginfo('template_url'); ?>/js/bootstrap.min.js"></script>
   <?php wp_footer();?>
  • 1
    Try using the wp_enqueue_scripts hook instead of init. See the Codex and this answer for details. Also, your wp_enqueue_scripts() call is missing either the $version parameter, or has $version set to true and does not set the $in_footer parameter. – Pat J Aug 15 '14 at 13:46
  • So I added some more information, in my post. But still no fix. – sanders Aug 15 '14 at 14:05
7

You're mixing up the singular wp_enqueue_script, which adds a script, with the plural wp_enqueue_scripts which is an action and a function that triggers that action. I think you want

function theme_scripts()
{
    wp_enqueue_script( 'custom-script', get_template_directory_uri() . '/js/menu-fix.js', array('jquery'), '1.0', true);
}
add_action('wp_enqueue_scripts', 'theme_scripts');

i.e. register against the enqueue_scripts-plural action that calls enqueue_script-singular.

  • 1
    This, and also remove that google jquery from your footer. WordPress has jquery included. You don't need a special version. – Otto Aug 17 '14 at 4:44
2

Try to remove this from footer.php

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

And One tip: Use wp_enqueue_script to load all JS on your web page.

1

The dependencies are case-sensitive - you need array( 'jquery' ) (no uppercase Q).

  • Ok so I changed it. But still no menu-fix.js – sanders Aug 15 '14 at 13:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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