• Resolved mathieuhays

    (@mathieuhays)


    Hi everyone,

    I don’t manage to use the wp_user_query output through jQuery.

    Array[1]
    0: Object
    >ID: 3
    >allcaps: Object
    >cap_key: "*****wp_capabilities"
    >caps: Object
    >data: Object
    >>ID: "3"
    >>display_name: "Machin"
    >>user_activation_key: ""
    >>user_email: "***"
    >>user_login: "machin"
    >>user_nicename: "machin"
    >>user_pass: "****"
    >>user_registered: "2013-07-04 12:20:26"
    >>user_status: "0"
    >>user_url: ""

    Why jQuery ?
    I’m getting this through an Ajax process (JSON) and i want to get all the ‘ID’, ‘user_login’, ‘display_name’ informations on jQuery.

    Thanks.

    PS: I spend my day looking for a solution without knowing which keywords would lead me to the right answer. Sorry if I double the entrie.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    Use the .parseJSON() method of jQuery to convert the JSON string into a javascript object. Then the values are available as object properties.

    var obj = jQuery.parseJSON('{"name":"John"}');
    alert( obj.name );

    Results in an alert box containing “John”.

    Thread Starter mathieuhays

    (@mathieuhays)

    Thank you bcworkz, but it’s not working.
    The object is too complex. I can’t apply that synthax for my case.
    When I write that stuff :
    obj.display_name
    The alert display ‘undefined’.
    I try some synthax which not working too:

    obj.data.display_name
    obj['data'].display_name

    I reproduce a simple version of my coding context :

    <?php
    /* Template Name: Test1 */
    
    get_header();
    
    $user = new WP_user_query('include=1');
    $final_user = $user->results;
    ?>
    <div class="info"><?php echo json_encode($final_user); ?></div>
    
    <script src="<?php bloginfo('template_directory'); ?>/js/jquery.js"></script>
    <script type="text/javascript">
    
    	$(document).ready(function(){
    
    		var usr = jQuery.parseJSON($('.info').html());
    		console.log(usr);
    
    		alert(usr.display_name);
    	});
    
    </script>
    <?php
    get_footer();
    ?>

    Thread Starter mathieuhays

    (@mathieuhays)

    I found the solution thanks to a friend.

    I get the informations through jQuery thanks to the code below:

    usr[0].data.display_name

    I hope it helps someone anyway !

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘manage wp user query output’ is closed to new replies.