0

I have a file in my theme's JS folder named cycle2.js.

  1. How do I include the Jquery from 'wordpress→includes' into my theme?

  2. How do I load this custom JavaScript file 'cycle2.js' into my theme?

    I have:

    function watercolor_js_scripts(){
    if (!is_admin()) {
    
        wp_register_script('cycle2', get_template_directory_uri() . '/js/cycle2.js', 'jquery', TRUE);
    
        wp_enqueue_script( 'jquery' );
        wp_enqueue_script( 'cycle2' );
    
    }
    add_action('init', 'watercolor_js_scripts');
    }
    

I can't understand any of the many explanations of this. Can some please just provide a copy/paste solution to throw into functions.php that will include the basic Jquery library included with WordPress & the cycle2.js file in my theme's js folder?

1

Your snippet seems mostly ok, except that dependency is a list and should be array('jquery'). Also wp_enqueue_scripts should be use as more appropriate.

You can check out Codex documentation for a complete example, see Example of Automatic Dependency Loading.

0

SOLVED: can't figure out the best way to update my question, sorry! Thank you Rarst the link you provided helped me figure this out:

add_action('wp_enqueue_scripts', 'watercolor_js_scripts');

function watercolor_js_scripts(){

    if (!is_admin()) {

        wp_register_script('cycle2', get_template_directory_uri() . '/js/cycle2.js', array('jquery'), TRUE);

        wp_enqueue_script( 'cycle2', get_stylesheet_directory_uri() . '/js/cycle2.js', array( 'jquery', 'my-script' ) );
}

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for?Browse other questions tagged or ask your own question.