I have created a wordpress website. I want to extract each user's data using php through his/her id.

I have created this code;

$userid = /* help me get the below input field's value */
$user_info = get_userdata($userId);
      echo 'Username: ' . $user_info->user_login . "\n";
      echo 'User roles: ' . implode(', ', $user_info->roles) . "\n";
      echo 'User ID: ' . $user_info->ID . "\n";
<input name="getUser" id="getUser" value=''/>

The user will write the id of the user he wants to extract his data. and then the data will echo back.

The code is working fine but i can't set the value of "getUser" input field to "$userId" variable. Also i want that php to re-execute on input field value change.


Please try below snippet of code.


<input name="getUser" id="getUser" value=''/>
<div id="user_data"></div>

JS (js/my-ajax-script.js)

    $('#getUser').keyup(function(e) {
      var user_id = e.target.value;
            url:my_ajax_object.ajaxurl, #add here your ajax url
             data:'action=get_this_user_data&user_id='+user_id ,


Ajax (add in functions.php)

function my_enqueue() {

wp_enqueue_script( 'ajax-script', get_template_directory_uri() . '/js/my-ajax-script.js', array('jquery') );

wp_localize_script( 'ajax-script', 'my_ajax_object',
        array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
   add_action( 'wp_enqueue_scripts', 'my_enqueue' );

add_action( 'wp_ajax_get_this_user_data', 'get_this_user_data' );
add_filter( 'wp_ajax_nopriv_get_this_user_data', 'get_this_user_data');
function get_this_user_data()
    $userid = $_POST["user_id"];
    $data = '';
    $user_info = get_userdata($userid);
       $data .= '<pre>';
       $data .= 'Username: ' . $user_info->user_login . '<br/>';
       $data .= 'User roles: ' . implode(', ', $user_info->roles) . '<br/>';
       $data .= 'User ID: ' . $user_info->ID . '<br/>';
       $data .='</pre>';
        $data = "user data not found";

      echo $data;

Hope this helps you!

| improve this answer | |
  • sorry, i'm newbie in the wordpress backend. I pasted the ajax code to the functions.php file and i change the url to my url = (wordpress\78-5) . Also i added the html code. After doing all this stuff, the input is not extracting data to the #userData div. Please can you figure out why? – Axmed nuur Jul 1 '19 at 14:31
  • I have updated code in my answer. please check now. i have added ajax url and create js file separated and enqueue in functions.php. let me know if any issue. – Chetan Vaghela Jul 1 '19 at 14:57
  • Can you please explain to me how i can add my ajax url? if i'm not wrong i made it like this ( url : '/wordpress/78-2',). Still the form is not working. – Axmed nuur Jul 1 '19 at 17:58
  • yes your are wrong. i have already added ajax url. you have to just create new my-ajax-script.js file in js folder. then add other code in functions.php file. i have already added ajax url my_ajax_object.ajaxurl in js function. you dont need to change now. just add in files and see it is working or not. – Chetan Vaghela Jul 1 '19 at 19:05
  • i copied your code exactly to the above files without changing anything, it doesn't work. sorry for my ignorance towards ajax. – Axmed nuur Jul 2 '19 at 13:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.