WordPress-使用jQuery ajax POST请求获取用户信息 [英] Wordpress - fetch user info using jQuery ajax POST request

查看:434
本文介绍了WordPress-使用jQuery ajax POST请求获取用户信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Wordpress中工作,试图使用ajax请求通过传递用户ID来获取用户数据.

I am working in Wordpress trying to use an ajax request to fetch user data by passing the user id.

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

I can see that the user id sends correctly via AJAX POST but I am getting an internal error message and I don't know why.

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

At first I thought it was because I was trying to fetch some custom fields that I had added to the user profile but even when I simplified my script I am still getting the error message.

非常感谢您的帮助!

前端

$('.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响应.

Mathieu added a hackable approach to intercepting a request and redirecting it, which is fine. I prefer to build out AJAX responses that return json_encoded arrays.

$('.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请求的功能.

Now we build out the function to handle our ajax requests.

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

First, we need to define our ajax add_action method ->

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'}中发送的动作".我们使用此钩子来验证我们的AJAX请求,它可以是您想要的任何内容.

We need to use both add_action lines here. I won't get into why. You'll notice the _ajax_request here. This is the 'action' that we sent over in our AJAX function data: {'action' : 'ajax_request'}. We use this hook to validate our AJAX request, it can be anything you'd like.

下一步,我们需要构建或运行ajax_handle_request.

Next, we'll need to build out or function 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;
  }
}

现在,让我们构建函数以实际获取作者元数据.

Now let's build our function to actually get the author meta.

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

[meta_option]是由WP的本地get_the_author_meta函数提供的字段.

Where [meta_option] is a field provided by WP's native get_the_author_meta function.

在这一点上,我们现在回到我们的success:function(data),(数据)是对我们返回的json_encoded数组的引用.现在,我们可以遍历该对象以获取我们的字段,并根据需要将其输出到页面中.

At this point, we'll now go back to our success:function(data) and (data) is a reference to the json_encoded array we've returned. We can now iterate over the object to get our fields and output them into the page as you'd like.

这篇关于WordPress-使用jQuery ajax POST请求获取用户信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆