在WordPress插件中使用Nonce和Ajax

在WordPress插件中使用Nonce和Ajax,wordpress,jquery,nonce,Wordpress,Jquery,Nonce,我试图在WordPress插件中使用nonce。我有一张表格,我想在上面用一下 在php中: function csf_enqueue() { //I have other scripts enqueued in htis function wp_enqueue_script('my-ajax-handle', plugin_dir_url(__FILE__).'file-path', array('jquery', 'jquery-ui-core', 'jquery-ui-datepick

我试图在WordPress插件中使用nonce。我有一张表格,我想在上面用一下

在php中:

function csf_enqueue() {

//I have other scripts enqueued in htis function 
wp_enqueue_script('my-ajax-handle', plugin_dir_url(__FILE__).'file-path', array('jquery', 'jquery-ui-core', 'jquery-ui-datepicker', 'google-maps'));

$data = array(
    'ajax_url' => admin_url( 'admin-ajax.php' ),
    'my_nonce' => wp_create_nonce('myajax-nonce')
);

wp_localize_script('my-ajax-handle', 'the_ajax_script', $data );

}

add_action('wp_enqueue_scripts', 'csf_enqueue');
add_action('wp_ajax_the_ajax_hook', 'the_action_function');
add_action('wp_ajax_nopriv_the_ajax_hook', 'the_action_function');
function the_action_function() {
   if( ! wp_verfiy_nonce( $nonce, 'myajax-nonce')) die ('Busted!');
//function continues
在jQuery文件中:

jQuery.post(the_ajax_script.ajaxurl, {my_nonce : the_ajax_script.my_nonce}, jQuery("#theForm").serialize() + "&maxLat="+ csf_dcscore_crime_map_bounds[0] + "&maxLong="+ csf_dcscore_crime_map_bounds[1] + "&minLat="+ csf_dcscore_crime_map_bounds[2] + "&minLong="+ csf_dcscore_crime_map_bounds[3],
                    function(response_from_the_action_function){
                        jQuery("#response_area").html(response_from_the_action_function);
                    });
我是否在jQuery中正确地发布了nonce

在php中:

function csf_enqueue() {

//I have other scripts enqueued in htis function 
wp_enqueue_script('my-ajax-handle', plugin_dir_url(__FILE__).'file-path', array('jquery', 'jquery-ui-core', 'jquery-ui-datepicker', 'google-maps'));

$data = array(
    'ajax_url' => admin_url( 'admin-ajax.php' ),
    'my_nonce' => wp_create_nonce('myajax-nonce')
);

wp_localize_script('my-ajax-handle', 'the_ajax_script', $data );

}

add_action('wp_enqueue_scripts', 'csf_enqueue');
add_action('wp_ajax_the_ajax_hook', 'the_action_function');
add_action('wp_ajax_nopriv_the_ajax_hook', 'the_action_function');
function the_action_function() {
   if( ! wp_verfiy_nonce( $nonce, 'myajax-nonce')) die ('Busted!');
//function continues
有什么建议吗?如果我去掉所有关于nonce的代码,一切正常。你知道为什么它不起作用吗?或者我如何调试它?谢谢大家!


谢谢。

有两件事不对

通过jQuery post方法发送数据,不能像以前那样发送一个对象+一个查询字符串。相反,您需要发送查询字符串格式或对象格式数据。为了方便起见,我将使用查询字符串格式。所以邮政编码应该是这样的

jQuery.post( the_ajax_script.ajaxurl, 
             jQuery("#theForm").serialize() + 
                    "&maxLat="+ csf_dcscore_crime_map_bounds[0] + 
                    "&maxLong="+ csf_dcscore_crime_map_bounds[1] + 
                    "&minLat="+ csf_dcscore_crime_map_bounds[2] + 
                    "&minLong="+ csf_dcscore_crime_map_bounds[3] +
                    "&my_nonce="+ the_ajax_script.my_nonce,
             function(response_from_the_action_function) {
                 jQuery("#response_area")
                     .html(response_from_the_action_function);
             });
这将发送参数my_nonce中的nonce。现在您可以替换服务器端

if( ! wp_verify_nonce( $nonce, 'myajax-nonce')) die ('Busted!');


查看和的文档将帮助您更好地:)

有两件事不对

通过jQuery post方法发送数据,不能像以前那样发送一个对象+一个查询字符串。相反,您需要发送查询字符串格式或对象格式数据。为了方便起见,我将使用查询字符串格式。所以邮政编码应该是这样的

jQuery.post( the_ajax_script.ajaxurl, 
             jQuery("#theForm").serialize() + 
                    "&maxLat="+ csf_dcscore_crime_map_bounds[0] + 
                    "&maxLong="+ csf_dcscore_crime_map_bounds[1] + 
                    "&minLat="+ csf_dcscore_crime_map_bounds[2] + 
                    "&minLong="+ csf_dcscore_crime_map_bounds[3] +
                    "&my_nonce="+ the_ajax_script.my_nonce,
             function(response_from_the_action_function) {
                 jQuery("#response_area")
                     .html(response_from_the_action_function);
             });
这将发送参数my_nonce中的nonce。现在您可以替换服务器端

if( ! wp_verify_nonce( $nonce, 'myajax-nonce')) die ('Busted!');


查看和的文档将更好地帮助您:)

Hi sbrajesh,非常感谢您的帮助。我对您建议的代码进行了更改。我还删除了“ajaxurl”中的下划线。不幸的是,我得到了“致命错误:调用未定义的函数wp\u verfiy\u nonce()”。听起来函数由于某种原因没有及时加载。有什么想法吗?非常感谢。我在wp_verify_暂时也有一个拼写错误。打字不好。非常感谢。你确定它在工作吗。如果它不更改“$data=array('ajax\u url'=>admin\u url('admin ajax.php'),'my\u nonce'=>wp\u create\u nonce('myajax-nonce'));”到“$data=array('ajaxurl'=>admin\u url('admin ajax.php'),'my\u nonce'=>wp\u create\u nonce('myajax-nonce'));”或者将我的代码中的
ajaxurl
替换为dataHi sbrajesh中的
ajax\u url
,非常感谢您的帮助。我对您建议的代码进行了更改。我还删除了“ajaxurl”中的下划线。不幸的是,我得到了“致命错误:调用未定义的函数wp\u verfiy\u nonce()”。听起来函数由于某种原因没有及时加载。有什么想法吗?非常感谢。我在wp_verify_暂时也有一个拼写错误。打字不好。非常感谢。你确定它在工作吗。如果它不更改“$data=array('ajax\u url'=>admin\u url('admin ajax.php'),'my\u nonce'=>wp\u create\u nonce('myajax-nonce'));”到“$data=array('ajaxurl'=>admin\u url('admin ajax.php'),'my\u nonce'=>wp\u create\u nonce('myajax-nonce'));”或者将我的代码中的
ajaxurl
替换为数据中的
ajax\u url