2015-02-06 51 views
0

首先,我倒了真的很愚蠢提交这个问题,因为有这么多的讨论它在无数的论坛上,但...我已经通过他们所有,我仍然无法得到它工作....不然我不能让它使用适当的方法WordPress的工作....wordpress在插件中使用.js脚本

我写一个插件与提交表单,我加入一个datepicker到提交表单

function datepickertester() { 
wp_enqueue_script('jquery-ui-datepicker'); 

wp_enqueue_style('datepicker', plugins_url('/js/css/datepicker.css', __FILE__)); 

?> 


<script type="text/javascript"> 

jQuery(document).ready(function() { 
    jQuery('.inputtable_date').datepicker({ 

    }); 



/* Danish initialisation for the jQuery UI date picker plugin. */ 
/* Written by Jan Christensen ([email protected]). */ 
(function(factory) { 
    if (typeof define === "function" && define.amd) { 

     // AMD. Register as an anonymous module. 
     define([ "../datepicker" ], factory); 
    } else { 

     // Browser globals 
     factory(jQuery.datepicker); 
    } 
}(function(datepicker) { 

datepicker.regional['da'] = { 
    closeText: 'Luk', 
    prevText: '&#x3C;Forrige<---', 
    nextText: '--->Næste&#x3E;', 
    currentText: 'Idag', 
    monthNames: ['Januar','Februar','Marts','April','Maj','Juni', 
    'Juli','August','September','Oktober','November','December'], 
    monthNamesShort: ['Jan','Feb','Mar','Apr','Maj','Jun', 
    'Jul','Aug','Sep','Okt','Nov','Dec'], 
    dayNames: ['Søndag','Mandag','Tirsdag','Onsdag','Torsdag','Fredag','Lørdag'], 
    dayNamesShort: ['Søn','Man','Tir','Ons','Tor','Fre','Lør'], 
    dayNamesMin: ['Sø','Ma','Ti','On','To','Fr','Lø'], 
    weekHeader: 'Uge', 
    showWeek: 'Uge', 
    dateFormat: 'dd-mm-yy', 
    firstDay: 1, 
    isRTL: false, 
    showMonthAfterYear: false, 
    yearSuffix: ''}; 
datepicker.setDefaults(datepicker.regional['da']); 

return datepicker.regional['da']; 

})); 
}); 
</script> 

<?php 
} 

上面的代码按照它的设想工作。 所以...它的工作,以及我知道这不是适当的wordpress方法来处理脚本,所以我已经将脚本移动到插件文件夹中的一个名为jscript-test.js的文件

因此继承人的问题,我试图注册脚本50次从论坛的讨论和教程,我发现在网络上,但没有人为我工作

显然我不会列出所有50个方法,我试图让它工作。 这里我尝试注册它的方式之一。

add_action('wp_register_script', 'script_registration'); 
function script_registration() { 
    wp_register_script('jscript-test', plugins_url('/js/jscript-test.js', __FILE__)); 
} 


function my_scripts_method() { 
wp_enqueue_script('jquery-ui-datepicker'); 
wp_enqueue_style('datepicker', plugins_url('/js/css/datepicker.css', __FILE__)); 
wp_enqueue_style('jscript-test'); 

} 

它是什么我做错了?

.........编辑.......

我摔倒了,我要补充一下我的插件一些更多的信息。 我打电话以下简码的功能,我的WordPress页面

,当我使用的第一个功能我的页面之上,在我的功能inputtable_func()它就像一个魅力,当 当我使用第二个代码之上在那里我试图在登记簿脚本名为.js其不工作的网页... 我给了一个例子,下面的底部代码上如何我调用这两个函数

function inputtable_func() { 
echo ' 

<form action="' . $_SERVER['REQUEST_URI'] . '" method="post"> 

<table><tr> 
<td class="inputtable_label_day"><label for="day">Dag</label></td> 
<td class="inputtable_text_dato"><label for="dato">Dato</label></td> 
<td class="inputtable_text_start"><label for="start">Start</label></td> 
<td class="inputtable_text_slut"><label for="slut">Slut</label></td> 
<td class="inputtable_text_pause"><label for="Pause">Pause</label></td> 
<td class="inputtable_text_egenb"><label for="egenb">Egen Betaling</label></td> 
<td class="inputtable_text_tot"><label for="tot">Total kørt ind</label></td> 
</tr><tr> 
<td class="inputtable_label_day"><label for="day">Mandag</label></td> 
<td class="inputtable_input_dateA"><input class="inputtable_date" type="text" name="dateA" value="' . (isset($_POST['dateA']) ? $dateA : null) . '"></td> 
<td class="inputtable_input_startA"><input class="inputtable_start" type="text" name="startA" value="' . (isset($_POST['startA']) ? $startA : null) . '"></td> 
<td class="inputtable_input_slutA"><input class="inputtable_slut" type="text" name="slutA" value="' . (isset($_POST['slutA']) ? $slutA : null) . '"></td> 
<td class="inputtable_input_pauseA"><input class="inputtable_pause" type="text" name="pauseA" value="' . (isset($_POST['pauseA']) ? $pauseA : null) . '"></td> 
<td class="inputtable_input_egenbA"><input class="inputtable_egenb" type="text" name="egenbA" value="' . (isset($_POST['egenbA']) ? $egenbA : null) . '"></td> 
<td class="inputtable_input_totA"><input class="inputtable_tot" type="text" name="totA" value="' . (isset($_POST['totA']) ? $totA : null) . '"></td> 
</tr><tr> 
<td class="inputtable_label_day"><label for="day">Tirsdag</label></td> 
<td class="inputtable_input_dateA"><input class="inputtable_date" type="text" name="dateB" value="' . (isset($_POST['dateB']) ? $dateB : null) . '"></td> 
<td class="inputtable_input_startA"><input class="inputtable_start" type="text" name="startB" value="' . (isset($_POST['startA']) ? $startA : null) . '"></td> 
<td class="inputtable_input_slutA"><input class="inputtable_slut" type="text" name="slutB" value="' . (isset($_POST['slutB']) ? $slutB : null) . '"></td> 
<td class="inputtable_input_pauseA"><input class="inputtable_pause" type="text" name="pauseB" value="' . (isset($_POST['pauseB']) ? $pauseB : null) . '"></td> 
<td class="inputtable_input_egenbA"><input class="inputtable_egenb" type="text" name="egenbB" value="' . (isset($_POST['egenbB']) ? $egenbB : null) . '"></td> 
<td class="inputtable_input_totA"><input class="inputtable_tot" type="text" name="totB" value="' . (isset($_POST['totB']) ? $totB : null) . '"></td> 
</tr><tr> 
<td class="inputtable_label_day"><label for="day">Onsdag</label></td> 
<td class="inputtable_input_dateA"><input class="inputtable_date" type="text" name="dateC" value="' . (isset($_POST['dateC']) ? $dateC : null) . '"></td> 
<td class="inputtable_input_startA"><input class="inputtable_start" type="text" name="startC" value="' . (isset($_POST['startC']) ? $startC : null) . '"></td> 
<td class="inputtable_input_slutA"><input class="inputtable_slut" type="text" name="slutC" value="' . (isset($_POST['slutC']) ? $slutC : null) . '"></td> 
<td class="inputtable_input_pauseA"><input class="inputtable_pause" type="text" name="pauseC" value="' . (isset($_POST['pauseC']) ? $pauseC : null) . '"></td> 
<td class="inputtable_input_egenbA"><input class="inputtable_egenb" type="text" name="egenbC" value="' . (isset($_POST['egenbC']) ? $egenbC : null) . '"></td> 
<td class="inputtable_input_totA"><input class="inputtable_tot" type="text" name="totC" value="' . (isset($_POST['totC']) ? $totC : null) . '"></td> 
</tr><tr> 
<td class="inputtable_label_day"><label for="day">Torsdag</label></td> 
<td class="inputtable_input_dateA"><input class="inputtable_date" type="text" name="dateD" value="' . (isset($_POST['dateD']) ? $dateD : null) . '"></td> 
<td class="inputtable_input_startA"><input class="inputtable_start" type="text" name="startD" value="' . (isset($_POST['startD']) ? $startD : null) . '"></td> 
<td class="inputtable_input_slutA"><input class="inputtable_slut" type="text" name="slutD" value="' . (isset($_POST['slutD']) ? $slutD : null) . '"></td> 
<td class="inputtable_input_pauseA"><input class="inputtable_pause" type="text" name="pauseD" value="' . (isset($_POST['pauseD']) ? $pauseD : null) . '"></td> 
<td class="inputtable_input_egenbA"><input class="inputtable_egenb" type="text" name="egenbD" value="' . (isset($_POST['egenbD']) ? $egenbD : null) . '"></td> 
<td class="inputtable_input_totA"><input class="inputtable_tot" type="text" name="totD" value="' . (isset($_POST['totD']) ? $totD : null) . '"></td> 
</tr><tr> 
<td class="inputtable_label_day"><label for="day">Fredag</label></td> 
<td class="inputtable_input_dateA"><input class="inputtable_date" type="text" name="dateE" value="' . (isset($_POST['dateE']) ? $dateE : null) . '"></td> 
<td class="inputtable_input_startA"><input class="inputtable_start" type="text" name="startE" value="' . (isset($_POST['startE']) ? $startE : null) . '"></td> 
<td class="inputtable_input_slutA"><input class="inputtable_slut" type="text" name="slutE" value="' . (isset($_POST['slutE']) ? $slutE : null) . '"></td> 
<td class="inputtable_input_pauseA"><input class="inputtable_pause" type="text" name="pauseE" value="' . (isset($_POST['pauseE']) ? $pauseE : null) . '"></td> 
<td class="inputtable_input_egenbA"><input class="inputtable_egenb" type="text" name="egenbE" value="' . (isset($_POST['egenbE']) ? $egenbE : null) . '"></td> 
<td class="inputtable_input_totA"><input class="inputtable_tot" type="text" name="totE" value="' . (isset($_POST['totE']) ? $totE : null) . '"></td> 
</tr><tr> 
<td class="inputtable_label_day"><label for="day">Lørdag</label></td> 
<td class="inputtable_input_dateA"><input class="inputtable_date" type="text" name="dateF" value="' . (isset($_POST['dateF']) ? $dateF : null) . '"></td> 
<td class="inputtable_input_startA"><input class="inputtable_start" type="text" name="startF" value="' . (isset($_POST['startF']) ? $startF : null) . '"></td> 
<td class="inputtable_input_slutA"><input class="inputtable_slut" type="text" name="slutF" value="' . (isset($_POST['slutF']) ? $slutF : null) . '"></td> 
<td class="inputtable_input_pauseA"><input class="inputtable_pause" type="text" name="pauseF" value="' . (isset($_POST['pauseF']) ? $pauseF : null) . '"></td> 
<td class="inputtable_input_egenbA"><input class="inputtable_egenb" type="text" name="egenbF" value="' . (isset($_POST['egenbF']) ? $egenbF : null) . '"></td> 
<td class="inputtable_input_totA"><input class="inputtable_tot" type="text" name="totF" value="' . (isset($_POST['totF']) ? $totF : null) . '"></td> 
<br></tr><tr> 
<td class="inputtable_label_day"><label for="day">Søndag</label></td> 
<td class="inputtable_input_dateA"><input class="inputtable_date" type="text" name="dateG" value="' . (isset($_POST['dateG']) ? $dateG : null) . '"></td> 
<td class="inputtable_input_startA"><input class="inputtable_start" type="text" name="startG" value="' . (isset($_POST['startG']) ? $startG : null) . '"></td> 
<td class="inputtable_input_slutA"><input class="inputtable_slut" type="text" name="slutG" value="' . (isset($_POST['slutG']) ? $slutG : null) . '"></td> 
<td class="inputtable_input_pauseA"><input class="inputtable_pause" type="text" name="pauseG" value="' . (isset($_POST['pauseG']) ? $pauseG : null) . '"></td> 
<td class="inputtable_input_egenbA"><input class="inputtable_egenb" type="text" name="egenbG" value="' . (isset($_POST['egenbG']) ? $egenbG : null) . '"></td> 
<td class="inputtable_input_totA"><input class="inputtable_tot" type="text" name="totG" value="' . (isset($_POST['totG']) ? $totG : null) . '"></td></tr> 
<tr><td class="inputtable_submit"><input type="submit" name="submit" value="send"></td></tr></table> 
</form>'; 

//datepickertester ...This work..... 
datepickertester(); 

// my_scripts_method .....this dossent work...... 
my_scripts_method(); 

} 

==== ================================================== =======

有点感谢kaloya n您已经有很大的帮助:)

我已低于最终结果得到它的工作像它应该在的情况下任何人都应该需要以此作为参考:)

function scripts_registration() { 
    include 'js/jscript-test.js'; 
    wp_enqueue_script('jquery-ui-datepicker'); 
    wp_enqueue_style('datepicker', plugins_url('/js/css/datepicker.css', __FILE__)); 
    wp_enqueue_script('jscript-test', plugins_url('/js/jscript-test.js', __FILE__)); 
} 
add_action('wp_enqueue_scripts', 'scripts_registration'); 
+0

难道添加任何脚本标记到页面?如果是的话,它的道路是什么?你是否在你的主题中加入了'wp_footer'? http://codex.wordpress.org/Function_Reference/wp_footer这是你的脚本在 – 2015-02-06 17:02:21

+0

中加载的地方。 和是的,我已经包括wp_footer在我的主题:) – Ghostetr 2015-02-06 17:56:16

回答

0

第一关您正在使用wp_register_script挂钩。这个钩子的坏处是......它不存在。 :)你应该使用wp_enqueue_scripts

第二件事,您尝试使用wp_enqueue_ 风格入队的脚本。你应该使用wp_enqueue_script

您可以将所有上面的代码为以下:

function scripts_registration() { 
    wp_enqueue_script('jquery-ui-datepicker'); 
    wp_enqueue_style('datepicker', plugins_url('/js/css/datepicker.css', __FILE__)); 
    wp_enqueue_script('jscript-test', plugins_url('/js/jscript-test.js', __FILE__)); 
} 
add_action('wp_enqueue_scripts', 'scripts_registration'); 

参考:

http://codex.wordpress.org/Function_Reference/wp_register_script http://codex.wordpress.org/Function_Reference/wp_enqueue_script http://codex.wordpress.org/Plugin_API/Action_Reference/wp_enqueue_scripts

+0

仍然无法工作 – Ghostetr 2015-02-06 17:54:31

+0

我编辑了我的答案,所以现在包含发生在一个单一的功能。如果仍然无法工作,请检查控制台是否出现错误,以及脚本文件是否包含在页面中。 – Kaloyan 2015-02-06 19:39:03

+0

感谢您的帮助kaloyan!我做了这个伎俩,现在它的工作就像一个魅力:) – Ghostetr 2015-02-07 13:50:07