admin管理员组文章数量:1323737
I have been trying to use wp_enqueue_script() to load a JS script into one of my pages but it keeps throwing a "Failed to load resource" 404 error in the console (running Chrome). My code is as follows:
myplugin.php:
function myplugin_load_script() {
wp_enqueue_script( 'myplugin-testjs', (plugin_dir_path( __FILE__ ) . "scripts/myplugin-test.js"), array('jquery'), null, true );
add_action('wp_enqueue_scripts', 'myplugin_load_script');
}
myplugin-test.js:
alert("Hello!");
I have checked the HTML tag and the development tools and both of them display the correct URL (eg. wp-content/plugins/myplugin/scripts/myplugin-test.js).
I have also checked that the file does exist there and the name is correct, PHP throws no errors either. Thanks for any help!
I have been trying to use wp_enqueue_script() to load a JS script into one of my pages but it keeps throwing a "Failed to load resource" 404 error in the console (running Chrome). My code is as follows:
myplugin.php:
function myplugin_load_script() {
wp_enqueue_script( 'myplugin-testjs', (plugin_dir_path( __FILE__ ) . "scripts/myplugin-test.js"), array('jquery'), null, true );
add_action('wp_enqueue_scripts', 'myplugin_load_script');
}
myplugin-test.js:
alert("Hello!");
I have checked the HTML tag and the development tools and both of them display the correct URL (eg. wp-content/plugins/myplugin/scripts/myplugin-test.js).
I have also checked that the file does exist there and the name is correct, PHP throws no errors either. Thanks for any help!
Share Improve this question edited Oct 5, 2017 at 8:26 cybmeta 20.6k5 gold badges47 silver badges57 bronze badges asked Oct 5, 2017 at 7:57 JuckixJuckix 131 silver badge3 bronze badges1 Answer
Reset to default 2Your syntax is wrong. The action must be defined outside of the callback.
Also, you are using plugin_dir_path()
function, which gets the path to plugin directoy in the filesystem of the server, not the url. You should use plugin_dir_url()
instead.
add_action( 'wp_enqueue_scripts', 'myplugin_load_script' );
function myplugin_load_script() {
wp_enqueue_script( 'myplugin-testjs', plugin_dir_url( __FILE__ ) . "scripts/myplugin-test.js", array('jquery'), null, true );
}
本文标签: 404 Error on trying to enqueue a JS file
版权声明:本文标题:404 Error on trying to enqueue a JS file 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742119361a2421623.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论