Home > Software design >  Problems with wp_enqueue_script in WordPress
Problems with wp_enqueue_script in WordPress

Time:01-11

I am quite new to WordPress and coding in general, so bear with me. I have two separate files of code one with all the php and another with all the JavaScript. I need the php to read the JavaScript but when I look in the console of the browser it can't find the file. It appears to be looking in the wrong folder so it won't find it anyways.

Here is the code that I have used to try get it to read the JavaScript file

function wpdocs_theme_name_scripts() {
    wp_enqueue_script( 'pq-forms-script', get_template_directory_uri() . '/pqnewsletterform.js', array(), '1.0.0', true );
} 

I've tried to change get_template_directory_uri to get_stylesheet_directory_uri but that did nothing.

Any suggestions on how to fix this problem would be much appreciated.

CodePudding user response:

Try this code.

You need to use plugin_dir_url( __FILE__ ) instead of get_stylesheet_directory_uri.

function enqueue_scripts() {
  wp_enqueue_script( 'form-js', plugin_dir_url( __FILE__ ) . 'pqnewsletterform.js', array( 'jquery' ), '1.0.0', true );
}
add_action( 'wp_enqueue_scripts', 'enqueue_scripts');
  •  Tags:  
  • Related