wordpress PHP with Ajax call

General Tech Bugs & Fixes 2 years ago

0 1 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Bugs & Fixes related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (1)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

 

Im looking to return user data back as a string through ajax with wordpress

Iv got the basic concept down

PHP

this is placed in my 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
         }
    })

 

the console log result is correct how ever its also logging lots of html elements that are not included in this. 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

 lang="en-US">
 id="loadtheme" class="loadtheme">
<head> etc etc etc....

 

Any thoughts?

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.