我在我的Wordpress-Plugin中使用jQuery/ajax。当我以这种方式链接jQuery时,它甚至可以工作
add_action('wp_head', 'hook_files');
function hook_files()
{
$output = "
<script src='https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js'></script>
<script src='//code.jquery.com/ui/1.11.2/jquery-ui.js'></script>
<link rel='stylesheet' href='//code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css'> ";
echo $output;
}
但我知道这不是正确的做法。所以我查找了文档并实现了这个:
function enqueue_my_scripts()
{
wp_enqueue_script( 'jquery' );
wp_enqueue_script( 'jquery-ui-core' );
}
add_action('wp_enqueue_scripts', 'enqueue_my_scripts');
但是它不再起作用了...有什么问题吗?
谢谢!
我建议使用enqueue来加载脚本,尤其是jquery,这样你就可以这样触发它: [推荐解决方案]
wp_enqueue_script('jquery');
或者一种更笨拙的方法:
function my_scripts() {
wp_enqueue_script( 'jquery' );
wp_register_style( 'prefix-style', plugins_url('mystyle.css', __FILE__) );
wp_enqueue_style( 'prefix-style' );
}
add_action('wp_enqueue_scripts','my_scripts');
除非你想使用 cdn 或 google repo for jquery,否则我建议执行以下操作:
wp_enqueue_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js', array('jquery'));