WordPress.org

Ready to get started?Download WordPress

Forums

Use Google Libraries
[resolved] Beginner's problem (4 posts)

  1. serons
    Member
    Posted 2 years ago #

    I have the following in functions.php

    function my_scripts() {
    	wp_deregister_script( 'jquery' );
    	wp_register_script( 'jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js', null, null, true );
    	wp_enqueue_script( 'jquery' );
    
    	wp_deregister_script( 'script_1' );
    	wp_register_script( 'script_1', get_template_directory_uri() . '/js/script_1.js', array( 'jquery' ), null, true );
    	wp_enqueue_script( 'script_1' );
    }
    add_action( 'wp_enqueue_scripts', 'my_scripts' );

    If UGL is enabled I get

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
    <script type="text/javascript">try{jQuery.noConflict();}catch(e){};</script>
    <script type="text/javascript" src="http://wp.dev/sitename/wp-content/themes/themename/js/script_1.js"></script>

    and the error

    Uncaught exception: TypeError: '$' is not a function
    Error thrown at line 7, column 0 in http://wp.dev/sitename/wp-content/themes/themename/js/script_1.js:
        $(function() {

    If UGL is not enabled the line <script type="text/javascript">try{jQuery.noConflict();}catch(e){};</script> is not generated and no errors are generated.

    From the last question in the FAQ I expect it to work in both cases. Why doesn't it?

    http://wordpress.org/extend/plugins/use-google-libraries/

  2. Jason Penney
    Member
    Plugin Author

    Posted 2 years ago #

    When you did this:

    wp_deregister_script( 'jquery' );
    	wp_register_script( 'jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js', null, null, true );
    	wp_enqueue_script( 'jquery' );

    you broke compatability with how WordPress uses jQuery. Please don't do this. You de-registered the stock jQuery, then re-registered an unsupported version, without all it's metadata present that core WordPress expects to function, and you told it to load in the footer, which is totally unsupported.

    By a fluke, when UGL was enable it restored some of that core functionality, which gives you the error (which is what you would get if you had used core WordPress without messing it up as well).

    Change your function to this:

    function my_scripts() {
    	wp_register_script( 'script_1', get_template_directory_uri() . '/js/script_1.js', array( 'jquery' ), null, true );
    	wp_enqueue_script( 'script_1' );
    }
    add_action( 'wp_enqueue_scripts', 'my_scripts' );

    And get your script working without UGL enabled, then enable UGL and it should still work.

    I'd guess you aren't writing your script to be compatable with jQuery's noConflict mode, and jQuery in WordPress (with or without UGL) is in noConflict mode.

    Short version: Don't just use $ you have to use jQuery. For more details see the jQuery docs.

  3. serons
    Member
    Posted 2 years ago #

    Thank you for your answer. My question is a reflection of my (now slightly lower) ignorance. I still get the try/catch line when UGL is enabled but errors are gone.

    Don't just use $ you have to use jQuery. For more details see the jQuery docs

    Thank you. I did a mindless s/\$\(/jQuery(/ and it seemed to have fixed complaints about $(). For reference the following page was useful, which also links to jQuery's docs: http://codex.wordpress.org/Function_Reference/wp_enqueue_script

    You de-registered the stock jQuery, then re-registered an unsupported version

    What do you mean by unsupported?

  4. Jason Penney
    Member
    Plugin Author

    Posted 2 years ago #

    I still get the try/catch line when UGL is enabled but errors are gone.

    As you should, because...

    What do you mean by unsupported?

    Firstly, the version of jQuery shipped with WordPress is modified, so it does not match what is on googles servers (the try/catch injection essentially makes them match up). Secondly, you aren't registering an exact version, and WordPress requires an exact version (also when you ask for 1.7 from google, vs 1.7.x, google sends a short time cache header, since the file is likely to change). You are also not registering it with all the same options as stock WordPress, so other parts of WordPress may break. And the stock registration may include extra data not included in the register call, and this can cause additional problems. UGL handles all this so you don't have to :)

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic