WordPress.org

Ready to get started?Download WordPress

Forums

Jquery only works when logged in (4 posts)

  1. JeremyEnglert
    Member
    Posted 1 year ago #

    For some reason, my jQuery scripts only seem to load when I am logged into WordPress. Here is the page: http://www.mcfaddengavender.net/newmg/

    As you can see, the images are not "sliding" in. However, if I login and return to the homepage, things work fine. Here is the code I'm using to run jQuery in the Functions.php:

    //jQuery - Use Google CDN instead of WP jQuery
    function my_scripts_method() {
    if (!is_admin()) {
        wp_deregister_script( 'jquery' );
        wp_register_script( 'jquery',     'http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js');
        wp_enqueue_script( 'jquery' );
     }
    }
    
    add_action('init', 'my_scripts_method');
    ?>

    Any ideas on what would cause such a weird issue?

  2. 3five
    Member
    Posted 1 year ago #

    It seems to be working fine for me.

    Try also using wp_enqueue_scripts instead of init for your action.

  3. JeremyEnglert
    Member
    Posted 1 year ago #

    I restored from an old backup and everything seems to be working fine now.

    However, what is the difference between "init" and "wp_enqueue_scripts"? I've seen them both used and they both seem to work.

    And thank you for taking the time to take a look! I appreciate it.

  4. 3five
    Member
    Posted 1 year ago #

    Hello Jeremy,

    There are two main differences between those two actions.

    1) init fires before wp_enqueue_scripts.
    2) wp_enqueue_scripts has more params which can check to see if a script is already loaded or if it has dependancies. Basically it handles scripts better that init.

    Using wp_enqueue_scripts is the correct way to do what you are doing. Here is a short article on this topic as well: http://wordpress.stackexchange.com/a/55928

    Glad to help.

    Cheers

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.