WordPress – 使用jQuery ajax POST请求获取用户信息

我正在使用Wordpress尝试使用ajax请求通过传递用户ID来获取用户数据。

我可以看到用户id通过AJAX POST正确发送但我收到内部错误消息,我不知道为什么。

起初我以为是因为我试图获取我添加到用户配置文件中的一些自定义字段,但即使我简化了我的脚本,我仍然收到错误消息。

任何帮助深表感谢!

前端

$('.author').click(function() { var id = $(this).attr('id'); var temp = id.split('-'); id = temp[1]; $.ajax({ type: 'POST', url: 'wp-content/themes/twentyeleven/author_info.php', data: {id: id}, dataType: 'html', success: function(data) { $('#author-bio').html(data); } }); return false; }); 

author_info.php

 $user_id = $_POST['id']; $forename = get_the_author_meta('user_firstname', $user_id); $output = $user_id; echo $output; 

错误信息

500 (Internal Server Error) jquery.min.js:4

Mathieu添加了一种可以拦截请求并重定向的黑客方法,这很好。 我更喜欢构建返回json_encoded数组的AJAX响应。

 $('.author').click(function() { var id = $(this).attr('id'); var temp = id.split('-'); id = temp[1]; $.ajax({ url: 'http://absolute.path/wp-admin/admin-ajax.php', data: {'action' : 'ajax_request', 'fn': 'getAuthorMeta', 'id': id}, dataType: 'json', success: function(data) { //We expect a JSON encoded array here, not an HTML template. } }); return false; }); 

现在我们构建了处理ajax请求的函数。

首先,我们需要定义我们的ajax add_action方法 – >

 add_action('wp_ajax_nopriv_ajax_request', 'ajax_handle_request'); add_action('wp_ajax_ajax_request', 'ajax_handle_request'); 

我们需要在这里使用add_action行。 我不会理解为什么。 你会在这里注意到_ajax_request 。 这是我们在AJAX函数data: {'action' : 'ajax_request'}发送的’action’ data: {'action' : 'ajax_request'} 。 我们使用这个钩子来validation我们的AJAX请求,它可以是你想要的任何东西。

接下来,我们需要构建或运行ajax_handle_request

 function ajax_handle_request(){ switch($_REQUEST['fn']){ case 'getAuthorMeta': $output = ajax_get_author_meta($_REQUEST['id']); break; default: $output = 'That is not a valid FN parameter. Please check your string and try again'; break; } $output = json_encode($output); if(is_array($output)){ return $output; }else{ echo $output; } } 

现在让我们构建我们的函数来实际获取作者元。

 function ajax_get_author_meta($id){ $theMeta = get_the_author_meta([meta_option], $id); return $theMeta; } 

其中[meta_option]是WP的本机get_the_author_meta函数提供的字段 。

在这一点上,我们现在回到我们的success:function(data)和(数据)是对我们返回的json_encoded数组的引用。 我们现在可以迭代对象来获取我们的字段并将它们输出到页面中,如您所愿。

我宁愿建议你使用WP AJAX动作方法 。

在您的情况下,将以下内容添加到functions.php文件中。

  add_action('wp_ajax_get_user_info', 'ajax_get_user_info'); add_action('wp_ajax_nopriv_get_user_info', 'ajax_get_user_info'); function ajax_get_user_info() { //Handle request then generate response using WP_Ajax_Response or your html. } 

然后在javascript标签。

 $('.author').click(function() { var id = $(this).attr('id'); var temp = id.split('-'); id = temp[1]; jQuery.post( ajaxurl, /* if you get error of undefined ajaxurl. set it to "http://example.com/wordpress/wp-admin/admin-ajax.php"*/ { 'action':'get_user_info', 'user_id':id }, function(response){ alert('The server responded: ' + response); } ); }); 

我建议你阅读在WordPress中使用AJAX的5个技巧 。

PS; 上面的代码未经过测试,可能有错误。 但是你明白了。

您当时没有处于POST状态,因为您正在调用模板的特定页面,该页面可能与您博客中的任何文章不对应。

相反,创建一个将执行此操作的插件:

 add_action('template_redirect', 'my_author_meta_intercept'); function my_author_meta_intercept(){ if(isset($_POST['getAuthorMeta'])){ echo get_the_author_meta('user_firstname', $_POST['getAuthorMeta']); exit(); } } 

当您使用以下命令调用它时,这会将请求短路到与之前相同的页面:

 http://mysite/mycurrenturl?getAuthorMeta=testMetaKey 

所以调用该post通常会像往常一样返回文章,但如果你传入?getAuthorMeta,它将停止选择模板,只返回你希望它返回的确切内容。

在您的页面中,您只需将您的javascript更改为:

 $('.author').click(function() { var id = $(this).attr('id'); var temp = id.split('-'); id = temp[1]; $.ajax({ type: 'POST', url: window.location.href, data: {getAuthorMeta: id}, success: function(data) { $('#author-bio').html(data); } }); return false; }); 

只需确保您根据需要调整概念!