Skip to content

Instantly share code, notes, and snippets.

@kodie
Last active September 22, 2020 18:11
Show Gist options
  • Save kodie/cfada9bc41d0dff0d5fa679296c4d162 to your computer and use it in GitHub Desktop.
Save kodie/cfada9bc41d0dff0d5fa679296c4d162 to your computer and use it in GitHub Desktop.
Gets user data in the form of an array with user meta attached, empty results filtered, and single results collapsed into a single value in WordPress
<?php
// Gets user data in the form of an array with user meta attached that has empty results filtered,
// and optionally single results collapsed into a single value and/or only single values returned.
function get_account_info($user_id = null, $collapse = true, $single = false) {
if (!$user_id) $user_id = get_current_user_id();
$user = get_userdata($user_id);
if (!$user) return false;
$user = $user->to_array();
$user['meta'] = array_filter(array_map(function ($m) use ($collapse, $single) {
if ($single || ($collapse && count($m) === 1)) return $m[0];
return $m;
}, get_user_meta($user_id)));
return $user;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment