Mark Mark - 1 year ago 51
Javascript Question

WP enqueue javascript

I'm making a simple WP Plugin with Index.php and jquery.js files.
How do I link the JS to work together. This is what I tried adding to the index.php, but it did not work. No JS from jquery.js appeared.

function my_scripts() {
wp_enqueue_script( 'script-name', get_template_directory_uri() . 'jquery.js', array(), '1.0.0', true );


The first thing to note is, like Meathanjay pointed out, your add_action needs to be outside the my_scripts() function or else it'll never execute.

get_template_directory_uri() is used to get the path to files in a theme. For a plugin, you need to use plugin_dir_url() and pass it the directory of a file in the plugin's main directory.

function my_scripts() { wp_enqueue_script( 'script-name', plugin_dir_url( __FILE__ ) . 'jquery.js', array(), '1.0.0', true ); } add_action('wp_enqueue_scripts','my_scripts');