Skip Navigation

[Closed] Information in USER API with field custom

This support ticket is created 3 years, 4 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 15 replies, has 2 voices.

Last updated by Christian Cox 3 years, 4 months ago.

Assisted by: Christian Cox.

Author
Posts
#1860111

I have created some custom fields for the user creation form, and I need those fields to appear in the wordpres API.

How do I do this?

#1860697

I searched Google for "wordpress register user meta field in user profile rest api" and found this article you may find useful:
https://imranhsayed.medium.com/add-custom-field-to-wordpress-rest-api-35c63c6dfff4

It shows how to register a custom field from the User's profile in the User REST API response. You return the formatted field value from the Types Field API types_render_usermeta, or return get_user_meta with the wpcf- slug prefix to return the raw field value from the database.

https://toolset.com/documentation/customizing-sites-using-php/functions/
Click "+More" to see examples of working with the types_render_usermeta API.

#1860739

Thanks Christian, I have included this function, but the field returns a null.

Could I know what I'm doing wrong?

//AÑADIR MOVIL AL API USER
    /**
     * Adds user_meta to rest api 'user' endpoint.
     */
    function adding_user_meta_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'] );
    }
#1860837

1. I can't tell what this function does:

get_telefono_movil

Is this a custom function you created? Where is it defined?

2. The callback function names do not match:

'get_callback'      => 'telefono_movil_callback',

...

function user_meta_callback

Those two callback names should be identical, right? Otherwise, I don't understand what you want to accomplish.

#1861249

Sure,

It should be like that.
But I don't know what you mean by this
The variable $telefono_movil is undefined: 'meta_value' => $telefono_movil,

//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'] );
    }
#1861589


The variable $telefono_movil is undefined: 'meta_value' => $telefono_movil,

That code was for another ticket about validation in Forms. This ticket is about not about validating Forms, it is about adding information to the User's REST API response. See my previous reply in this ticket for details about the errors in your custom code: https://toolset.com/forums/topic/information-in-user-api-with-field-custom/#post-1860837

#1861595

Could you help me to define this variable?

#1861697

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.

#1861721

Good afternoon Christian, sorry to mix up the tickets. Yes, the '12345' appears.

Could you help me with the get_user_meta function?

Thanks

#1861779

The documentation for the get_user_meta function is available on the WordPress site here: https://developer.wordpress.org/reference/functions/get_user_meta/

To access a Types field using get_user_meta, you must use a wpcf- prefix with the field slug. So if your User field slug in wp-admin is some-field-slug, you can access the raw field value for User ID 123 like this:

$field_value = get_user_meta( 123, 'wpcf-some-field-slug', true );
#1861887

Thanks Christian, but I can't understand it.

If I want to show the data of all the users with the custom ones that I include with toolset. How should I do the function?

$all_meta_for_user = array_map( function( $a ){ return $a[0]; }, get_user_meta( $user_id ) );
print_r( $all_meta_for_user );
#1861919

I don't really understand, but it seems like you would have to query all Users, then loop over all the Users somehow and output their usermeta fields. The looping part and the User query part has nothing to do with Toolset. It's pure WordPress and PHP. If you can show me a working example that queries all Users and outputs some hard-coded value for each User instead of the Types field, like I showed before using 12345, I can show you how to plug in a Toolset custom field value into that output.

#1861979

Ok, forget this part. I only need the phone field to show me in the users information when I consult the users API

#1861983

You can see how to get a Types User field value using the get_user_meta function in my previous comment here: https://toolset.com/forums/topic/information-in-user-api-with-field-custom/#post-1861779

Was there a problem implementing this code? If so, please share the updated code in your next reply.

#1861993
//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,
            )
        );
    }
$field_value = get_user_meta('wpcf-telefono_movil', true );
    /**
     * 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 'wpcf-telefono_movil';
    }

The topic ‘[Closed] Information in USER API with field custom’ is closed to new replies.