I would prefer to focus on the topic of this ticket, which is adding information to the User's REST API response. Please do not refer to code in another ticket that has nothing to do with this ticket, it is just confusing everything. Here is the code we are discussing in this ticket:
//AÑADIR MOVIL AL API USER
/**
* Adds user_meta to rest api 'user' endpoint.
*/
function adding_telefono_movil_rest() {
register_rest_field( 'user',
'telefono_movil',
array(
'get_callback' => 'telefono_movil_callback',
'update_callback' => null,
'schema' => null,
)
);
}
/**
* Return user meta object.
*
* @param array $user User.
* @param string $field_name Registered custom field name ( In this case 'user_meta' )
* @param object $request Request object.
*
* @return mixed
*/
function user_meta_callback( $user, $field_name, $request) {
return get_telefono_movil( $user['id'] );
}
I've already mentioned problems in this code that must be resolved: https://toolset.com/forums/topic/information-in-user-api-with-field-custom/#post-1860837
I think you should adjust the code like so:
add_action( 'rest_api_init', 'adding_telefono_movil_rest' );
//AÑADIR MOVIL AL API USER
/**
* Adds user_meta to rest api 'user' endpoint.
*/
function adding_telefono_movil_rest() {
register_rest_field( 'user',
'telefono_movil',
array(
'get_callback' => 'telefono_movil_callback',
'update_callback' => null,
'schema' => null,
)
);
}
/**
* Return user meta object.
*
* @param array $user User.
* @param string $field_name Registered custom field name ( In this case 'user_meta' )
* @param object $request Request object.
*
* @return mixed
*/
function telefono_movil_callback( $user, $field_name, $request) {
return 12345;
}
This should return a value of 12345 in the telefono_movil field in the User's REST API response. Can you see 12345 in the response after adjusting the code? If not, there is a problem in your custom code that is unrelated to Toolset. You need help with basic PHP and a WordPress API, not a Toolset API, so you should consult the WordPress documentation for assistance with that code.
Once the code returns 12345 in the REST API, you will replace 12345 in the code with some other function to return the custom field value. There are two functions I can help with:
The WordPress function get_user_meta will return the raw value of a custom field from the database.
https://developer.wordpress.org/reference/functions/get_user_meta/
The Types API function types_render_usermeta will return a formatted value from a Types custom field.
https://toolset.com/documentation/customizing-sites-using-php/functions/
Click "+More" to see examples of working with the types_render_usermeta API.
However, your current code uses an undefined function called get_telefono_movil. This function does not exist in WordPress or in Toolset, so I have no idea what it's supposed to do. You should replace it with one of the two options I described, or explain in more detail what this custom function does. My guess is you made some copy + paste error in this function name.