2014-02-14 20 views
0

我能够注册和排队我自己的下载jquery-ui插件与控制台上的错误关于它。 jquery-ui-core由于某种原因无法使用相同的方法加载。麻烦enqueueing内置jQuery UI脚本在WordPress中

控制台也抱怨说我的$不是$(document).ready(function)初始jquery包装函数。即使wp正在调用自己的内核版本的jquery,我也会得到这个结果。

狩猎文档显示,wp还包括jquery-ui脚本的各种各样的。所以我走了那条路,但他们没有被使用他们的手柄叫。

我做错了什么?

 wp_enqueue_script('jquery-core'); 
     //wp_register_script('jquery-core', get_template_directory_uri() . '/js/jquery.ui.core.min.js', array('jquery')); 
     wp_enqueue_script('jquery-ui-core'); 
     //wp_register_script('jquery-draggable', get_template_directory_uri() . '/js/jquery.ui.draggable.min.js', array('jquery')); 
     wp_enqueue_script('jquery-ui-draggable'); 
     //wp_register_script('jquery-droppable', get_template_directory_uri() . '/js/jquery.ui.droppable.min.js', array('jquery')); 
     wp_enqueue_script('jquery-ui-droppable'); 
     // wp_register_script('jquery-mouse', get_template_directory_uri() . '/js/jquery.ui.mouse.min.js', array('jquery')); 
     wp_enqueue_script('jquery-ui-mouse'); 
     // wp_register_script('jquery-sortable', get_template_directory_uri() . '/js/jquery.ui.sortable.min.js', array('jquery')); 
     wp_enqueue_script('jquery-ui-sortable'); 
     //wp_register_script('jquery-widget', get_template_directory_uri() . '/js/jquery.ui.widget.min.js', array('jquery')); 
     wp_enqueue_script('jquery-ui-widget'); 

目前,我有我的register_script函数调用评论指出,做工作的时候我加入了enqueue_script其句柄调用它,但控制台被扔我的错误有关这些文件(从jQuery的UI下载)我的身影它的bc jquery没有被识别,因此$没有被定义的错误。

如果我首先使用wp_register_script调用脚本,它实际上会加载,但会引发很多指向jquery-ui库的控制台错误。最后,最后一个错误实际上是我的代码,说$(...)。accordion();不是一个功能。我从jquery-ui演示中提取了这段代码。我可以在jsfiddle中单元测试我的jquery-ui构建,并且这一切都很好。我没有手动调用jquery-ui库,因为他们有一个下拉选择它,但我丢弃了html和js演示代码。

有什么想法?

回答

1

有关$未定义的错误是因为jQuery在WP中以无冲突模式加载。

替换:

$(document).ready(function(){ 

有了:

jQuery(document).ready(function($){ 

也只是为了确保这是你的PHP与排队挂钩脚本里面的函数。

function wpse_load_js() { 
    wp_enqueue_script('jquery-ui-core'); 
    wp_enqueue_script('jquery-ui-draggable'); 
    wp_enqueue_script('jquery-ui-droppable');  
    wp_enqueue_script('jquery-ui-mouse'); 
    wp_enqueue_script('jquery-ui-sortable'); 
    wp_enqueue_script('jquery-ui-widget'); 
} 
add_action('wp_enqueue_scripts', 'wpse_load_js'); 
+0

感谢您的jQuery非冲突模式部分,修复了这一点。为了调用脚本,我已经将脚本包装在函数中,并且不会加载。我知道这个函数可以工作,但我正在修改一个工作主题,并在其中添加了这些脚本。 – appthat