I have a loop to retrieve all users with the specific meta_key user values from wp_user_meta database and then put it in wp_localize_script array so I could access the data with Javascript and do my thing with it. Unfortunately, when I run the loop and put all the results to the variable, and try to console.log the results, I get just the string "Array" out in the console 3 times in one line, instead of the real values.

enter image description here

Here is my code:


function Load_Template_Scripts_wpa83855(){

    $args = array(
        'meta_key' => 'quiz_scores'

    $users = get_users($args);
    foreach($users as $user){
            echo "<pre>";
        print_r(get_user_meta ($user->ID));
                $veik .= get_user_meta ($user->ID);
            echo "</pre>";
    if ( is_page_template('page-quiz.php') ) {
        wp_enqueue_script( 'quiz', get_template_directory_uri() . '/js/script.js', array(), '20151215', true );
                wp_localize_script('quiz', Questions_list, array(
                    'siteUrl' => get_option("question"),
                    "ans" => get_option("answer"),
                    'ajax_url' => admin_url( 'admin-ajax.php' ),
                    'usr_scr' => $veik

print_r(get_user_meta ($user->ID)); works just fine, I get my all 3 users printed out with the meta_key "quiz_scores". But when I put it in the variable and then use it inside script localize like this: 'usr_scr' => $veik and try to console.log it in my other script.js file console.log(Questions_list.usr_scr); like I said, I get only string "Array" out in the console 3 times (as many times as there is users in database with that specific meta_key instead of the real value). I tried using toString() but it doesn't do anything. Any ideas?

  • please remove dot into this ($veik .=) Dec 13, 2017 at 3:56
  • @PatelJignesh If I remove it, I get only last element from the loop to my variable, because on each iteration it gers overwritten.
    – Limpuls
    Dec 13, 2017 at 3:57
  • define the variable $veik = ' ' after that you can use like this $veik .= ' ' without variable define can't use it like this. Dec 13, 2017 at 4:02
  • .= is a string operator, you can't use it to append arrays.
    – Milo
    Dec 13, 2017 at 4:03
  • @PatelJignesh I defined it first and used like this again $veik .= get_user_meta ($user->ID); but same problem
    – Limpuls
    Dec 13, 2017 at 4:08

1 Answer 1


get_user_meta returns an array, so to get an array containing an array of each user's data, use [] to append:

foreach( $users as $user ){
    $veik[] = get_user_meta( $user->ID );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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