我正在尝试开发我的第一个Wordpress插件,为此我想使用wp_localize_script发布表单数据而不刷新页面。 问题是我不太了解如何在wp_localize_script中设置$ data参数。
更多解释:
1)我有一个包含每个复选框的数据列表的表单。
2)当我点击按钮保存已检查的数据时,我尝试将它们发布在admin-ajax.php上,但我不知道如何设置我的第三个参数function wp_localize_script()。
在Wordpress Codex的示例中,似乎必须知道第三个数据......但是我不知道在点击保存按钮时检查了哪些数据。
在Wordpress Codex上:
<?php
wp_localize_script( $handle, $name, $data );
// Register the script
wp_register_script( 'some_handle', 'path/to/myscript.js' );
// Localize the script with new data
$translation_array = array(
'some_string' => __( 'Some string to translate', 'plugin-domain' ),
'a_value' => '10'
);
wp_localize_script( 'some_handle', 'object_name', $translation_array );
// Enqueued script with localized data.
wp_enqueue_script( 'some_handle' );
?>
在我的插件中,我想通过在json(字符串化)中传递已发布的数据来发布一系列已检查数据,但它还没有工作(可能是因为我不了解wp_localize_script(如何) )可以在这种情况下工作。)
例如,我的脚本如下所示:
- Jquery部分:
// clic on the "save button"
$('#bppv_phcats_save').click(function(){
// I load a div to show a loader ( only for the design )
$('.bppv-loader').slideDown(150);
// I create an empty array
var ph_cats = [];
// I make a loop to check which checkbox is checked
$('.bppv_phcat').each(function(i, obj) {
// If current checkbox in loop is checked, I push it in the array with the data 1
if($(this).prop('checked') === true){
ph_cats[$(this).attr('name')] = 1;
// Else if the current checkbox is unchecked, I push it into the array too but with the data 0
}else{
ph_cats[$(this).attr('name')] = 0;
}
});
// I convert my array in json format and stringify this
ph_cats = JSON.stringify($.extend({},ph_cats));
// I test in the web console if the data looks like I want, it's ok
console.log(ph_cats);
// I instanciate the variables
var data = {
action : 'select_phcats',
nonce : bppv_phcats_obj.nonce, // I create a nonce for more security
ph_cats : bppv_phcats_obj.ph_cats // This is the data I want to retrieve in PHP
};
// I post the data to adamin-ajax.php
$.post(bppv_phcats_obj.ajax_url, data, function(response){
// I display an alert to check the response
alert('Response : '+response);
});
});
- PHP部分 1)注册.js脚本
// I register my .js script
wp_register_script('bppv_js',BPPV_ROOT_URL.'assets/js/bppv.js',array('jquery'),'1.0',true);
// I enqueue it
wp_enqueue_script('bppv_js');
// The famous wp_localize_script() function I don't understand
wp_localize_script('bppv_js','bppv_phcats_obj',array('ajax_url' => admin_url('admin-ajax.php'), ??? ));
2)我的PHP函数来获取发布的数据
function select_phcats(){
global $wpdb;
$nonce = $_POST['nonce'];
if (!wp_verify_nonce($nonce,'ajax-nonce')){ die ( 'hum… It seems there is a problem…'); }
if(isset($_POST['ph_cats'])){
$PHcats = json_decode($_POST['ph_cats'],true);
$PHcats = $_POST['ph_cats'];
echo 'YEAH! I get my json data!';
}else{
echo 'shit! It doesn't work!';
}
wp_die();
}
当然我无法在PHP端获取我的数据。我试图在wp_localize_script()中指定第三个参数,如下所示:
wp_localize_script('bppv_js','bppv_phcats_obj',array('ajax_url' => admin_url('admin-ajax.php'),'nonce' => wp_create_nonce('ajax-nonce'),'ph_cats' => isset($_POST['ph_cats']) ? $_POST['ph_cats'] : 'no posted data'));
wp_localize_script('bppv_js','bppv_phcats_obj',array('ajax_url' => admin_url('admin-ajax.php'),'nonce' => wp_create_nonce('ajax-nonce'),'ph_cats' => $_POST['ph_cats']));
也是这样:
wp_localize_script('bppv_js','bppv_phcats_obj',array('ajax_url' => admin_url('admin-ajax.php'),'nonce' => wp_create_nonce('ajax-nonce'),'ph_cats' => $PHcats));
我试着看看这是否可以在没有设置wp_localize_script()中的数据的情况下工作,如下所示:
wp_localize_script('bppv_js','bppv_phcats_obj',array('ajax_url' => admin_url('admin-ajax.php')));
没有什么对我有用,在网上不同教程的几个主题中,我注意到有些人没有在wp_localize_script()中设置第三个参数,但在Wordpress Codex中,这个第三个参数似乎是被要求......所以我不明白......
有关我在Wordpress 4.4.2上运行脚本的信息。
有人知道我该怎么办?
感谢您阅读我的问题,并提前感谢您的回复。
Cordialy,
BBFUNK01;)
答案 0 :(得分:0)
我认为你不需要使用wp_localize_script来做你正在尝试做的事情。
为了访问PHP端的POST数据,您需要使用wp_ajax_(action)钩子和/或wp_ajax_nopriv_(action)。第一个钩子用于管理员侧ajax,第二个钩子用于面向观看者的一侧。一旦使用其中一个或两个挂钩注册功能,POST数据将自动可用。
例如:
add_action( 'wp_ajax_select_phcats', 'select_phcats' );
add_action( 'wp_ajax_nopriv_select_phcats', 'select_phcats' );
修改强>
我明白为什么你要尝试使用wp_localize_script。您似乎想要将对象用于Nonce值和ajax_url。在管理员方面,应该已经为您定义了javascript变量ajaxurl。如果这是在前端,请添加:
add_action('wp_head','pluginname_ajaxurl');
function pluginname_ajaxurl() {
?>
<script type="text/javascript">
var ajaxurl = '<?php echo admin_url('admin-ajax.php'); ?>';
</script>
<?php
}
然后你的ajax请求看起来像这样:
$.post(ajaxurl, data, function(response){
// I display an alert to check the response
alert('Response : '+response);
});
至于使用nonce进行安全性(这是一个好主意!),你可以将nonce放在一个隐藏的输入字段中。 Here is an example.
我认为您可以像这样使用wp_localize_script传递nonce和ajax url:
$nonce = wp_create_nonce( 'my-nonce' );
wp_localize_script('bppv_js','bppv_phcats_obj',array('ajax_url' =>
admin_url('admin-ajax.php'), 'nonce'=> $nonce));
有关如何使用wp_localize_script的更多信息,请查看this tutorial.