在Wordpress中包含jquery UI

I have tried to get jquery-ui working with wordpress with no luck. Can someone point me in the right direction? I've dug up a ton of SO resources and blogs explaining it, but so far can't get this working. However when I include the scripts directly in the page itself I can get the datepicker to function properly. I'm trying to make the table rows draggable (and eventually droppable).

Here is what I currently have-

In /themes/twentysixteen/functions.php

I followed the steps here and enqueued jquery-ui functions Correctly enqueue jquery-ui. I then added a couple of my own scripts based on the information here Add jquery scripts

/* Used to include jquery-ui scripts
*  Taken from http://xl-websites.com/how-to-enqueue-jquery-or-jquery-ui-correctly-in-wordpress/
*
*/

function add_jquery_ui() {
        wp_enqueue_script('jquery-ui-core');
        wp_enqueue_script('jquery-ui-widget');
        wp_enqueue_script('jquery-ui-mouse');
        wp_enqueue_script('jquery-ui-accordion');
        wp_enqueue_script('jquery-ui-autocomplete');
        wp_enqueue_script('jquery-ui-slider');
        wp_enqueue_script('jquery-ui-tabs');
        wp_enqueue_script('jquery-ui-sortable');
        wp_enqueue_script('jquery-ui-draggable');
        wp_enqueue_script('jquery-ui-droppable');
        wp_enqueue_script('jquery-ui-datepicker');
        wp_enqueue_script('jquery-ui-resize');
        wp_enqueue_script('jquery-ui-dialog');
        wp_enqueue_script('jquery-ui-button');
}
add_action( 'wp_enqueue_scripts', 'add_jquery_ui' );

function custom_js() {
        wp_enqueue_script('date', get_template_directory_uri() . '/js/date.js', array('jquery'), false, true);
        wp_enqueue_script('drag', get_template_directory_uri() . '/js/drag.js', array('jquery'), false, true);
}
add_action('wp_enqueue_scripts', 'custom_js');

With those addded, I created the scripts in my js folder-

/js/drag.js

(function() {
        $(".drag").draggable({
                helper: function(event) {
                        return $('<div class="drag-row"><table></table></div>').find('table').append($(event.target).closest('tr').clone()).end();
                },
        });
});

And /js/date.js

( function() {
        $( "#datepicker" ).datepicker();
});

In my php page I added the date id to a calendar, and a class id to the table row as targets for the jquery scripts.

<p>Date: <input type="text" id="datepicker"></p>

        echo "<div id='table'>";
        echo "<table class='listing'>";
                echo "<thead>";
                        echo "<tr>";
                                echo "<th><b>Course Name</b></th>";
                                echo "<th><b>Fiscal Year</b></th>";
                                echo "<th><b>Course Number</b></th>";
                                echo "<th><b>Start Date</b></th>";
                                echo "<th><b>End Date</b></th>";
                        echo "</tr>";
                echo "</thead>";
                echo "<tbody>";
                        foreach($courses as $course){
                                echo "<tr class='drag'>";
                                        echo "<td>".$course->course_name."</td>";
                                        echo "<td>".$course->fy."</td>";
                                        echo "<td>".$course->course_number."</td>";

                                        $date = $course->course_date_start;
                                        $date = strtotime($date);

                                        if ($date){
                                                echo "<td>".date("m/d/Y", $date)."</td>";
                                        } else {
                                                echo "<td></td>";
                                        }

                                        $date = $course->course_date_end;
                                        $date = strtotime($date);

                                        if ($date){
                                                echo "<td>".date("m/d/Y", $date)."</td>";
                                        } else {
                                                echo "<td></td>";                                               }
                                        echo "</tr>";
                        }
                echo "</tbody>";
        echo "</table>";

When my page loads, the scripts are all there (including jquery, I put spaces since the jquery lines are at line 350, and the jquery-ui lines are at line 500), but the ui isn't functional. Anyone have any insights?

<script type='text/javascript' src='##/wp-includes/js/jquery/jquery.js?ver=1.12.4'></script>
<script type='text/javascript' src='##/wp-includes/js/jquery/jquery-migrate.min.js?ver=1.4.1'></script>




<script type='text/javascript' src='##/wp-content/themes/twentysixteen/js/functions.js?ver=20160816'></script>
<script type='text/javascript' src='##/wp-includes/js/jquery/ui/core.min.js?ver=1.11.4'></script>
<script type='text/javascript' src='##/wp-includes/js/jquery/ui/widget.min.js?ver=1.11.4'></script>
<script type='text/javascript' src='##/wp-includes/js/jquery/ui/mouse.min.js?ver=1.11.4'></script>
<script type='text/javascript' src='##/wp-includes/js/jquery/ui/accordion.min.js?ver=1.11.4'></script>
<script type='text/javascript' src='##/wp-includes/js/jquery/ui/position.min.js?ver=1.11.4'></script>
<script type='text/javascript' src='##/wp-includes/js/jquery/ui/menu.min.js?ver=1.11.4'></script>
<script type='text/javascript' src='##/wp-includes/js/wp-a11y.min.js?ver=4.6.1'></script>
<script type='text/javascript'>
/* <![CDATA[ */
var uiAutocompleteL10n = {"noResults":"No search results.","oneResult":"1 result found. Use up and down arrow keys to navigate.","manyResults":"%d results found. Use up and down arrow keys to navigate."};
/* ]]> */
</script>
<script type='text/javascript' src='##/wp-includes/js/jquery/ui/autocomplete.min.js?ver=1.11.4'></script>
<script type='text/javascript' src='##/wp-includes/js/jquery/ui/slider.min.js?ver=1.11.4'></script>
<script type='text/javascript' src='##/wp-includes/js/jquery/ui/tabs.min.js?ver=1.11.4'></script>
<script type='text/javascript' src='##/wp-includes/js/jquery/ui/sortable.min.js?ver=1.11.4'></script>
<script type='text/javascript' src='##/wp-includes/js/jquery/ui/draggable.min.js?ver=1.11.4'></script>
<script type='text/javascript' src='##/wp-includes/js/jquery/ui/droppable.min.js?ver=1.11.4'></script>
<script type='text/javascript' src='##/wp-includes/js/jquery/ui/datepicker.min.js?ver=1.11.4'></script>
<script type='text/javascript'>
jQuery(document).ready(function(jQuery){jQuery.datepicker.setDefaults({"closeText":"Close","currentText":"Today","monthNames":["January","February","March","April","May","June","July","August","September","October","November","December"],"monthNamesShort":["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"],"nextText":"Next","prevText":"Previous","dayNames":["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"],"dayNamesShort":["Sun","Mon","Tue","Wed","Thu","Fri","Sat"],"dayNamesMin":["S","M","T","W","T","F","S"],"dateFormat":"MM d, yy","firstDay":1,"isRTL":false});});
</script>
<script type='text/javascript' src='##/wp-includes/js/jquery/ui/resizable.min.js?ver=1.11.4'></script>
<script type='text/javascript' src='##/wp-includes/js/jquery/ui/button.min.js?ver=1.11.4'></script>
<script type='text/javascript' src='##/wp-includes/js/jquery/ui/dialog.min.js?ver=1.11.4'></script>
<script type='text/javascript' src='##/wp-content/themes/twentysixteen/js/date.js?ver=4.6.1'></script>
<script type='text/javascript' src='##/wp-content/themes/twentysixteen/js/drag.js?ver=4.6.1'></script>
<script type='text/javascript' src='##/wp-includes/js/wp-embed.min.js?ver=4.6.1'></script>

</div>
douji5746
douji5746 是的,我擦除了域名并将其替换为#
接近 4 年之前 回复
douliangbian7323
douliangbian7323 包含在wordpress管理员中的jquerydraggable不适用于前端。
接近 4 年之前 回复
dongliu0823
dongliu0823 URL前面的##/是什么,只是跳过域或......?
接近 4 年之前 回复

1个回答



您可以告诉 wp_enqueue_script()</ code>函数您的脚本依赖于jQuery </ strong> ,所以它们将以正确的方式和顺序插入,注意第三个参数,例如:</ p>

  wp_enqueue_script('jquery-ui-core',false,array('jquery)  ')); 
</ code> </ pre>

了解更多此处。</ p>

另请注意, jQuery UI-Effects </ code>句柄中不包含 jQuery UI Effects </ code> 。</ p>
</ div>

展开原文

原文

You can tell the wp_enqueue_script() function that your scripts depend on jQuery, so they will be inserted in correct way and order, notice the third parameter, for example:

 wp_enqueue_script( 'jquery-ui-core', false, array('jquery'));

Read more here.

Also note that jQuery UI Effects is not included with the jquery-ui-core handle.

doulubashang6936
doulubashang6936 应用于jquery ui核心行的enqueue_script,这不起作用
接近 4 年之前 回复
duanlan7239
duanlan7239 我在functions.js中的custom_js函数中指定了它:wp_enqueue_script('date',get_template_directory_uri()。'/ js / date.js',array('jquery'),false,true);
接近 4 年之前 回复
douping3891
douping3891 指定相反的:您的脚本依赖于jQuery。
接近 4 年之前 回复
duanhe4267
duanhe4267 我的理解是ui脚本处理了jquery依赖,因为ui带有word press。 我从jjery-ui包含的例子不正确吗? 我指定jquery是我的自定义脚本的依赖项
接近 4 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问