简体   繁体   中英

Problems with wp_enqueue_script in WordPress

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.

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');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM