我正在开发一个名为“我的搜索网站”的插件,其中我有网站链接。如果单击该链接,将调用jQuery函数,并将该jQuery变量传递给wordpress函数,从中可以将其添加到user meta。
我的jQuery如下所示:
$(".blavatar").click(function(){
//toggle between the star icon
$(this).toggleClass(\'star-empty star_filled\');
//retrieve the clicked link\'s ID and it\'s href
var fav_id = $(this).parent().parent().attr(\'id\');
fav = $(this).parent().parent();
//append favorites using ajax call
jQuery.ajax({
type:"POST",
url: ajaxurl + "?action=fdiAddFavorite",
data: {
action: \'fdiAddFavorite\',
fav_id: fav_id,
},
success:function(data){
$(\'#wp-admin-bar-my-favorite-list\').append($(fav.html()));
console.log(data);
//console.log(fav_id);
},
error: function(errorThrown){
alert(errorThrown);
}
//}).always(function() { alert("complete");
});
return false;
});
在同一页上,我的wordpress函数声明如下:
add_action(\'wp_ajax_fdiAddFavorite\', \'fdiAddFavorite\');
add_action(\'wp_ajax_nopriv_fdiAddFavorite\', \'fdiAddFavorite\');
function fdiAddFavorite() {
$fav_id = get_post($_REQUEST[\'fav_id\']);
$user_id = get_current_user_id();
$favorite_list = get_user_meta($user_id, \'favorite links\', true);
//print_r($favorite_list);
if(empty($favorite_list))
$favorite_list = array();
$favorite_list[] = $fav_id;
update_user_meta($user_id, \'favorite links\', $favorite_list);
//sort($favorite_list);
//print_r($favorite_list);
die(\'{"success": true}\');
}
单击事件后,ajax返回0,并且该函数未被调用,并且ajax调用无法识别该函数。我无法更新用户元。你能帮我一下吗。谢谢