• I have looked at various tutorials and whatnot but am still having trouble getting it to load.

    In my functions.php file I have:

    function my_scripts() {
        wp_deregister_script( 'jquery' );
        wp_register_script( 'jquery', 'https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js');
        wp_enqueue_script( 'jquery' );
    }    
    
    add_action('wp_enqueue_scripts', 'my_scripts');

    And then in my test file I simply have the following:

    if (typeof jQuery != 'undefined') {
        alert("jQuery library is loaded!");
    }else{
        alert("jQuery library is not found!");
    }

    When I hardcode the script call into header.php, the test works fine. When I attempt to use the function above, I get the not found error and there is no script tag in the HTML header.

    Am I missing something?

Viewing 14 replies - 1 through 14 (of 14 total)
  • Hey.

    It is not advised to load Google’s version of jQuery in WordPress. WordPress have their own version of jQuery. It’s considered best practise for all WordPress plugins and themes to use this version as it is set to noConflict() mode.

    You should have the following in your functions.php file:

    add_action( 'wp_enqueue_script', 'load_jquery' );
    function load_jquery() {
        wp_enqueue_script( 'jquery' );
    }
    Thread Starter zakkath

    (@zakkath)

    I changed the function to that but the code in the test file still says jQuery is not loaded.

    Could you possibly provide a link to your site?

    Thread Starter zakkath

    (@zakkath)

    It’s on a localhost server at the moment.

    But my functions.php has the code you provided and the index.php of the theme is as follows

    <!doctype html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Test</title>
    <link rel="stylesheet" type="text/css" href="https://localhost/mta/wp-content/themes/mta/style.css" />
    </head>
    
    <body>
    <div id="wrapper">
    
    <header id="masthead">
    	<h1>Test</h1>
    	<nav id="mainnav">
        </nav>
    </header>
    <section id="contentarea">
    
    <script type="text/javascript">
    
    if (typeof jQuery != 'undefined') {
    
        alert("jQuery library is loaded!");
    
    }else{
    
        alert("jQuery library is not found!");
    
    }
    </script>
    </section>
    
    <footer id="pagefooter">
    </footer>
    
    </div>
    </body>
    </html>
    Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    Let us know when you can provide a webpage demonstrating the issue.

    Thread Starter zakkath

    (@zakkath)

    Unfortunately that will not be possible at the moment. I will just hard code it in and be done with it.

    Moderator Ipstenu (Mika Epstein)

    (@ipstenu)

    ?????? Advisor and Activist

    Can you put all the code for the plugin up somewhere? Github?

    Moderator Jose Castaneda

    (@jcastaneda)

    THEME COFFEE MONKEY

    What does your header.php file look like? For some weird reason I have a feeling the wp_head hook isn’t being used.

    Thread Starter zakkath

    (@zakkath)

    Right now my header.php looks as follows:

    <!doctype html>
    <html>
    <head>
    <meta charset="utf-8">
    <title><?php bloginfo('name'); ?></title>
    </head>
    <body>
    
    <header id="pageheader">
    	<h1 id="masthead"><?php bloginfo('name'); ?></h1>
    	<nav id="headernav">
        </nav>
    </header>

    Moderator Jose Castaneda

    (@jcastaneda)

    THEME COFFEE MONKEY

    Had a feeling.

    Just before the end </head> add wp_head(). That is what will enqueue all your scripts.

    Thread Starter zakkath

    (@zakkath)

    I added that and it still did not work. After looking around I found some other tutorials and, based on them, I changed the following:

    add_action( 'wp_enqueue_script', 'load_jquery' );

    Became

    add_action( 'init', 'load_jquery' );

    Moderator Ipstenu (Mika Epstein)

    (@ipstenu)

    ?????? Advisor and Activist

    If wp_enqueue_script isn’t getting called, that’s pretty much where your problems are starting.

    <!doctype html>
    <html>
    <head>
    <meta charset="utf-8">
    <title><?php bloginfo('name'); ?></title>
    <?php wp_head(); ?>
    </head>
    <body>

    Does your footer have wp_footer() in there?

    Thread Starter zakkath

    (@zakkath)

    Yes. I never have used wp_head() before. A lot of the older theme tutorials never included it.

    I added it though. Had to make some minor adjustments as the damn bar from the admin backend showed up.

    Thread Starter zakkath

    (@zakkath)

    Ok, I have every thing up and running and things are working well. But one thing is puzzling me that I have not found a clear answer for:

    Why does this not working using the wp_head hook? If I have:

    function load_jquery() {
        wp_enqueue_script('jquery', '', '', null, '');
    }
    add_action('wp_head', 'load_jquery');

    Nothing happens, but if I use the ‘init’ hook I get the script tag loading jQuery in my page header.

    Is using init like this good coding practice?

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘Getting Google jQuery to Load’ is closed to new replies.