Javascript 如何使用wp_enqueue_脚本加载js?
我正在尝试使用Javascript 如何使用wp_enqueue_脚本加载js?,javascript,php,jquery,wordpress,Javascript,Php,Jquery,Wordpress,我正在尝试使用wp\u enqueue\u脚本调用js库。但是我也不知道我在这里犯了什么错误,我想用插件,请告诉我如何使用我的插件目录 add_action('wp_enqueue_scripts', 'add_custom_script'); function add_custom_script(){ wp_enqueue_script( 'jquery-custom-script', get_template_directory_uri().'/js/jquery-cu
wp\u enqueue\u脚本
调用js库。但是我也不知道我在这里犯了什么错误,我想用插件,请告诉我如何使用我的插件目录
add_action('wp_enqueue_scripts', 'add_custom_script');
function add_custom_script(){
wp_enqueue_script(
'jquery-custom-script',
get_template_directory_uri().'/js/jquery-custom-script.js'
);
}
有人能猜出我在这里搞错了什么吗 您可以使用函数
示例:
/**
* Include CSS file for MyPlugin.
*/
function myplugin_scripts() {
wp_register_script( 'foo-js', plugin_dir_url( __FILE__ ) . 'assets/foo-custom.js' );
wp_enqueue_script( 'foo-js' );
}
add_action( 'wp_enqueue_scripts', 'myplugin_scripts' );
如果您将
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu>作为获取当前PHP脚本文件路径的参数传递如果您想从插件加载JS,比如说在plugins/my plugin目录中,并且代码直接在plugins/my plugin中的文件中运行,请执行以下操作(还添加了一个参数,明确指定jQuery作为脚本的依赖项):
就这样做吧:
wp_enqueue_script('custom-js', get_template_directory_uri() . '/custom.js', array('jquery'), null, true);
在wordpress插件中插入js和css的最佳方法是
首先注册文件,然后使用它们
// register scripts and style on initialization
add_action('init', 'register_style');
function register_style() {
wp_register_style( 'style', plugins_url('/css/style.css', __FILE__), false, '1.0.0', 'all');
wp_register_script( 'scripts', plugins_url('/js/scripts.js', __FILE__));
}
// use the registered scripts and style above
add_action('wp_enqueue_scripts', 'enqueue_style');
function enqueue_style(){
wp_enqueue_style( 'style' );
wp_enqueue_script('scripts');
}
@AnandChoudhary如果此答案对您有帮助,请将其标记为已批准,以便对其他人有帮助。
// register scripts and style on initialization
add_action('init', 'register_style');
function register_style() {
wp_register_style( 'style', plugins_url('/css/style.css', __FILE__), false, '1.0.0', 'all');
wp_register_script( 'scripts', plugins_url('/js/scripts.js', __FILE__));
}
// use the registered scripts and style above
add_action('wp_enqueue_scripts', 'enqueue_style');
function enqueue_style(){
wp_enqueue_style( 'style' );
wp_enqueue_script('scripts');
}