0

Is there anyway to get the same details as /users/me api command but in WP backend?

I have backend function that would get the user meta details

custom api /user_meta

public myFunc( $userId ) {
    $user_meta = get_user_meta( $userId );

    return $user_meta
}

The user meta field has an ACF option that is formatted strange

"place": [
    "a:1:{i:0;s:12:\"Earth\";}"
],

But when using the /users/me API call, the return is

/users/me default wp api

"place": [
    "Earth"
],

Although I could use a conjunction of both apis to get the proper user meta details, it isn't ideal. Is there anyway of getting the /users/me api call in wordpress backend?

1 Answer 1

0
"a:1:{i:0;s:12:\"Earth\";}"

This is a PHP serialized value. If you pass it to unserialize you'll get back the array you're expecting. (And it looks like you've edited it because "Earth" is not 12 characters long: that should be s:5:"Earth".)

However I'm surprised that get_user_meta doesn't already do this for you: it passes the value it's fetched from the database through maybe_unserialize() before it's returned to you, which ought to do this automatically for you. Is there any chance you've double-serialized this before storing the string in the database? (And note also the warning on the documentation page about unserializing arbitrary user-provided strings.)

To answer the general question though, everything you can get from /users/me is available with normal API calls and you don't need to use the REST API from inside WordPress itself.

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.