使用Ajax调用的wordpress PHP

我正在寻找通过Wordpress通过Ajax将用户数据作为字符串返回的方式


iv掌握了基本概念


的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


    $.ajax({

         url: "http://www.example.com/",

         method: "POST",

         data: {

             getbyID: "23",

         },

         success: function(response){

             console.log(response);

             //example script logic here

         }

    })

控制台日志结果是正确的,但是它还会记录许多未包含在其中的html元素。如


我不完全清楚为什么。


这是一个完整的小例子


Username: someusername

User roles: subscriber

User ID: 23

<!DOCTYPE html>

<html>

<div id="loadtheme" class="loadtheme"></div>

<head>


etc etc etc....

有什么想法吗?


繁华开满天机
浏览 136回答 2
2回答

倚天杖

回声后尝试退出或返回。我不知道完整的问题,但希望对您有所帮助add_action('template_redirect', 'edit_user_concept');&nbsp; &nbsp; function edit_user_concept(){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $id = $_POST['getbyID'];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $user_info = get_userdata($id);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo 'Username: ' . $user_info->user_login . "\n".'User roles: ' . implode(', ', $user_info->roles) . "\n".'User ID: ' . $user_info->ID . "\n";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exit;&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript