• Resolved tkse

    (@tkse)


    Hi.

    I’m having trouble adding jQuery to my website without breaking other scripts like the dropdown when logged in and plugins that also uses jQuery.

    Currently I have linked jQuery for my theme in functions.php like this:

    /* 	Load jQuery for own script	*/
    function my_init() {
    	if (!is_admin()) {
    		wp_deregister_script('jquery');
    		wp_register_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js', false, '1.3.2', true);
    		wp_enqueue_script('jquery');
    
    		// load a JS file from my theme: js/theme.js
    		wp_enqueue_script('my_script', '/wp-content/themes/rksgolf/js/theme.js', array('jquery'), '1.0', true);
    	}
    }
    add_action('init', 'my_init');

    This works fine for my scripts, but breaks others. How can I have mine co-exist with others?

Viewing 10 replies - 1 through 10 (of 10 total)
  • Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    What JavaScript errors are you getting? Deregistering jQuery is a big thing you know.

    Thread Starter tkse

    (@tkse)

    You can have a look: http://rksgolf.no

    Currently, my own script is running the slider in the sidebar, while at the bottom of the page I have another slider run by a plugin, which doesn’t show up as long as I link jQuery in the functions.php file. Same thing with the adminbars dropdown menu.

    Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    I would just use the version of jQuery that pretty much all themes, plugins and the core application rely on.

    Thread Starter tkse

    (@tkse)

    Example code?

    I changed the code to:

    if (!is_admin()) add_action("wp_enqueue_scripts", "my_jquery_enqueue", 11);
    function my_jquery_enqueue() {
       wp_deregister_script('jquery');
       wp_register_script('jquery', "http" . ($_SERVER['SERVER_PORT'] == 443 ? "s" : "") . "://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js", false, null);
       wp_enqueue_script('jquery');
    }

    Ignoring my own .js file in functions.php and called the script within sidebar.php instead. Works for now at least.

    robbieobbie

    (@robbieobbie)

    Andrew is right, deregistering the current WordPress version of JQuery is never good practice if you are relying on the use of plugins. Plugin authors work hard to keep their plugins up to date with the most recent versions of WordPress and this might be their biggest pet peeve as outdated versions of JQuery easily break them.

    Thread Starter tkse

    (@tkse)

    So how should I go about adding custom jQuery?

    Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    I don’t understand, why do you have to use your own jQuery library?

    Thread Starter tkse

    (@tkse)

    Don’t I? I guess I’ve looked it up at some point, read about the enqueue_script and always assumed you had to.

    Can I just delete the above code from functions.php and my script should still work?

    Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    Can you do this instead:

    /* 	Load jQuery for own script	*/
    function my_init() {
    	if (!is_admin()) {
    		// load a JS file from my theme: js/theme.js
    		wp_enqueue_script('my_script', '/wp-content/themes/rksgolf/js/theme.js', array('jquery'), '1.0', true);
    	}
    }
    add_action('init', 'my_init');

    Thread Starter tkse

    (@tkse)

    Excellent. Thanks! πŸ™‚

Viewing 10 replies - 1 through 10 (of 10 total)

The topic ‘Adding jQuery without breaking stuff’ is closed to new replies.