简体   繁体   English

使用Ajax调用的wordpress PHP

[英]wordpress PHP with Ajax call

Im looking to return user data back as a string through ajax with wordpress 我正在寻找通过Wordpress通过Ajax将用户数据作为字符串返回的方式

Iv got the basic concept down iv把基本概念记下来

PHP 的PHP

this is placed in my functions.php 这放在我的functions.php中

 add_action('template_redirect', 'edit_user_concept'); function edit_user_concept(){ $id = $_POST['getbyID']; $user_info = get_userdata($id); echo 'Username: ' . $user_info->user_login . "\\n"; echo 'User roles: ' . implode(', ', $user_info->roles) . "\\n"; echo 'User ID: ' . $user_info->ID . "\\n"; } 

Jquery/JS jQuery / JS

  $.ajax({ url: "http://www.example.com/", method: "POST", data: { getbyID: "23", }, success: function(response){ console.log(response); //example script logic here } }) 

the console log result is correct how ever its also logging lots of html elements that are not included in this. 控制台日志结果是正确的,但是它还会记录许多未包含在其中的html元素。 Such as

im not exactly sure why. 我不完全清楚为什么。

here is a small example to large to post in its full 这是一个完整的小例子

 Username: someusername User roles: subscriber User ID: 23 <!DOCTYPE html> <html lang="en-US"> <div id="loadtheme" class="loadtheme"></div> <head> etc etc etc.... 

Any thoughts? 有什么想法吗?

Try exit or return after echo.I dont know the complete problem but hope this helps 回声后尝试退出或返回。我不知道完整的问题,但希望对您有所帮助

add_action('template_redirect', 'edit_user_concept');
    function edit_user_concept(){
          $id = $_POST['getbyID'];
          $user_info = get_userdata($id);
          echo 'Username: ' . $user_info->user_login . "\n".'User roles: ' . implode(', ', $user_info->roles) . "\n".'User ID: ' . $user_info->ID . "\n";
          exit;
    }

You are echoing the html without stopping it anywhere, you can use json response here, for json , first of all you need to use dataType: "json" in your ajax request as: 您正在回显html而不在任何地方停止它,您可以在此处使用json响应,对于json ,首先需要在ajax请求中使用dataType: "json"

 url: "http://www.example.com/",
 method: "POST",
 dataType: "json" // need to use datatype here.
 data: {
     getbyID: "23",
 }

Now, you need to store data in an array in your PHP as: 现在,您需要将数据存储在PHP中的array中,如下所示:

$result = array();
$result['Username'] = $user_info->user_login;
$result['User_roles'] = implode(', ', $user_info->roles);
$result['User_id'] = $user_info->ID;

Now, you can use json_encode() : 现在,您可以使用json_encode()

echo json_encode($result);

You can print json result as like: 您可以像这样打印json结果:

console.log(response.Username); // will print username
console.log(response.User_roles); // will print user roles
console.log(response.User_id); // will print user id

Complete Example: 完整的例子:

$.ajax({
     url: "http://www.example.com/",
     method: "POST",
     dataType: "json" // json data type
     data: {
         getbyID: "23",
     },
     success: function(response){
         console.log(response.Username);
         console.log(response.User_roles);
         console.log(response.User_id);
         //example script logic here
     }
 });

PHP: PHP:

<?php
function edit_user_concept(){
  $id = $_POST['getbyID'];
  $user_info = get_userdata($id);
  $result = array();
  $result['Username'] = $user_info->user_login;
  $result['User_roles'] = implode(', ', $user_info->roles);
  $result['User_id'] = $user_info->ID;
  echo json_encode($result); // will return json response
}
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM