sanosuke
sanosuke

Reputation: 19

WP theme jquery

How do you add custom javascript/jquery to WordPress theme? I tried the steps on this site http://codex.wordpress.org/Function_Reference/wp_enqueue_script with no luck.

function theme_scripts() {
    wp_enqueue_script( 'sample', get_template_directory_uri() . '/js/sample.js', array( 'jquery' ) ); 
}

add_action( 'wp_enqueue_scripts', 'theme_scripts');

Upvotes: 0

Views: 64

Answers (1)

Rohil_PHPBeginner
Rohil_PHPBeginner

Reputation: 6080

As explained in codex, you need to define your jQuery through wp_enqueue_script.

So for theme it would be something like this :

    add_action('wp_enqueue_scripts', 'fwds_scripts');

    function fwds_scripts() {
        wp_enqueue_script('jquery');

        wp_register_script('slidesjs_core', get_template_directory_uri('js/jquery.slides.min.js', __FILE__), array("jquery"));
        wp_enqueue_script('slidesjs_core');


        wp_register_script('slidesjs_init', get_template_directory_uri('js/slidesjs.initialize.js', __FILE__));
        wp_enqueue_script('slidesjs_init');

    }

Here what I have defined wp_enqueue_script as jQuery. You can skip this step also.

Second step, I have register my jQuery/JavaScript.In that slidejs_core is the handle which is unique to define that script.Second is URL. You need to first write get_template_directory_uri(which is URL to your theme) and them other path where you haev store your JS. I have made JS folder and in that folder I have saved my JS file .So my path is js/jquery.slides.min.js. Then I have defined Array, which is helpful to load that particular script before our script.This array is optional.

After you need to add wp_enqueue_script through your handles which we defined in wp_register_script.

Upvotes: 2

Related Questions