Displaying user meta data (array) only displaying first value

105 Views Asked by At

I have phone call details being added to corresponding user's meta data via webhooks (using wp-webhook plugin). The output is an array as data is always being added. For some reason only the first value is being shown on the front end. Below are 2 separate methods I've tried and neither show all of the data. I am also using PHP Code Snippets plugin (not sure if this has anything to do with the issue). The output meta data from the webhook shown in the user's profile looks like this:

//output data
    array (
      0 => 'phone_data 1',
      1 => 'phone data 2',
      2 => 'phone data 3',
      3 => 'phone data 4',
      4 => 'phone data 5',
      5 => 'phone data 6',
    )


//get and display user meta
$current_user = wp_get_current_user();
if ( $current_user ) {
    $meta = get_user_meta( $current_user->ID, 'phone_calls' , true );
    if ( ! is_array( $meta ) ) {
    $meta = array();
}
        echo 'User phone calls: ' . $current_user->phone_calls . ;
}


// Also Tried this:

$current_user = wp_get_current_user();
echo 'User phone calls: ' . $current_user->phone_calls . ;
1

There are 1 best solutions below

0
On

Your code is broken. Here's a valid version of it, even though you could simplify it by using the WordPress-internal function get_current_user_id().

//get and display user meta
$current_user = wp_get_current_user();
if ( $current_user ) {
    $meta = get_user_meta( $current_user->ID, 'phone_calls' , true );

    if ( ! is_array( $meta ) ) {
        $meta = array();
    }

    echo 'User phone calls: ' . json_encode( $meta );
}

You can also use the following function (directly within WP Webhooks) to get the user meta data: https://wp-webhooks.com/integrations/wordpress/actions/get_user/