WordPress.org

Ready to get started?Download WordPress

Forums

Add jquery script the right way (8 posts)

  1. baszer
    Member
    Posted 2 years ago #

    Hello,

    I have a small script which needs jqueary, so I add this to my header:

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

    now it works, but I think it is not the right way to do it, I think it needs to be done with wp_enqueue_scripts

    but when I add this to function.php

    <?php function my_slider() {
        wp_deregister_script( 'jquery' );
        wp_register_script( 'jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js');
        wp_enqueue_script( 'jquery' );
    	}    
    
    	add_action('wp_enqueue_scripts', 'my_slider'); ?>

    it doesn't work,
    so what do I forget?

  2. potentweb
    Member
    Posted 2 years ago #

    Put:
    <?php wp_enqueue_script("jquery"); ?>
    before the
    <?php wp_head(); ?> in your header.

  3. baszer
    Member
    Posted 2 years ago #

    doesn't work, is my script ok?

  4. potentweb
    Member
    Posted 2 years ago #

    What is your site's url?

  5. baszer
    Member
    Posted 2 years ago #

    it's locally runned

  6. Yojance
    Member
    Posted 2 years ago #

    Your code looks good to me. The only thing I can think of is that you might be missing wp_head(); on your header.php file right before </head>

  7. Yojance
    Member
    Posted 2 years ago #

    Actually, I just noticed that you mention you put this on a file named function.php. The correct file name should be functions.php.

    That might be your problem after all.

  8. s_ha_dum
    Member
    Posted 2 years ago #

    Try registering it as something other than 'jquery'-- something like 'gjquery'. I have had trouble registering scripts if I use a name that matches something WordPress uses. I don't know if it is a bug or feature :) I haven't had time to look into it much.

Topic Closed

This topic has been closed to new replies.

About this Topic