Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

External javascript not loading in Wordpress

I'm trying to load a custom js file on wordpress, I've upload to my javascript theme folder and I'm using the following code in functions.php but I can't make it work:

function wpb_adding_scripts() {
    wp_register_script('service-graph', plugins_url('js/service-graph.js', __FILE__), array('jquery'),'1.1', true);
    wp_enqueue_script('service-graph');
}

add_action( 'wp_enqueue_scripts', 'wpb_adding_scripts' ); 

Thank you.

like image 708
Nation Avatar asked Jul 01 '26 17:07

Nation


1 Answers

You need to use get_template_directory_uri() function for get the theme folder path.then you can pass js/yourjsfile path name.

Try below code

<?php

    function wpb_adding_scripts() {
    wp_register_script('service-graph', get_template_directory_uri() . '/js/service-graph.js', array('jquery'),'1.1', true);
    wp_enqueue_script('service-graph');
    }

    add_action( 'wp_enqueue_scripts', 'wpb_adding_scripts' );  
    ?>

Jfyi - if you put anything in your current active theme folder you must need to use get_template_directory_uri() function as its return path to your theme directory.

like image 127
Manthan Dave Avatar answered Jul 03 '26 05:07

Manthan Dave



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!