I got it. Turns out the tutorial I was looking at was old and I was using the wrong WP function. I was using register_api_field but the correct one to use is register_rest_field.
It goes like this…
function facebook_add_user_data() {
register_rest_field( 'user',
'facebook',
array(
'get_callback' => 'rest_get_user_field',
'update_callback' => null,
'schema' => null,
)
);
}
add_action( 'rest_api_init', 'facebook_add_user_data' );
function rest_get_user_field( $user, $field_name, $request ) {
return get_user_meta( $user[ 'id' ], $field_name, true );
}
I tried it out and the response from the server included the “facebook” field and the URL from the user’s profile.
Also, the facebook_get_user_field function can actually be reused so I renamed it rest_get_user_field and tested it with another field name nad it produced that data in the response too.
Related Posts:
- How to loop through JSON data in wordpress WP REST API
- Create a new user using WP REST API and declare meta object
- JSON REST API WordPress only showing first 10 categories
- How to clone all WordPress Rest API end points
- REST API custom endpoints for metaboxes
- cURL 28 error after switch from to brew php 7.2 on localhost
- Creating a post with the REST API, curl and oauth returning 401 error
- WP API V2 returning Invalid User ID
- Does the REST API (official) support custom post types?
- How to make an meta_query optional?
- Passing JSON data from WP Query into AJAX causing NULL errors
- Export all post from database to JSON only when the database gets updated
- API JSON Data in WordPress
- fetch_feed: retrieve entries in the appearing order, not chronologically
- wordpress custom endpoint multiple params
- register_rest_route regex option for base64 or alternate
- Gutenberg custom block using only register_block_type() and HTML
- How can I export post data in xml format?
- using woocommerce_template_single_add_to_cart in shop-loop – javascript issues [closed]
- how to decode % in app [closed]
- How to generate HMAC-SHA1 signature to use with WP REST API and OAuth1
- Why does running get_the_excerpt() when generating JSON file take 28 seconds vs. 599 milliseconds without it?
- How to send user data in json format to another server when user register on wordpress site in PHP
- How do I fetch all comments per post via WP REST API?
- Create dynamic pages from external JSON data without storing in Database?
- Parsing php string in jquery [closed]
- Vagrantpress + composer
- woocommerce registration form with klaviyo(don’t work with current user)
- WordPress 5 WP REST routes – No errors
- wp_remote_get adding backslashes
- Is there an equivalent to WP_Error object I can return in the case of a successful REST request?
- Creating a Multi-Level Associative Object Using AJAX
- what is the best practice to add new field to an api route
- How to show single category archive
- wp_enqueue_script seperately for each shortcode
- how to save selected option in variable for rest api category filter
- database sent to a JSON file
- Input data from email form not going to JSON file
- PHP > Scheduled Tasks > Sending daily email with dynamic API variables
- Fetching instagram api not working on wordpress
- Buddypress update user avatar image via REST
- Building a REST API for your web app exposes primary keys of DB records?
- How can I spin up a new website for a registered user automatically?
- Simple WordPress endpoint route doesn’t work
- Is it possible to create new user from external form using REST API?
- Cant register rest routs from class instance
- WordPress REST API register_rest_route give a 500 error
- Custom route and query
- Validate and Sanitize WP REST API Request using WP JSON Schema?
- Filter by field with array value in ACF on WP REST API
- Retrieve Json data and create multiple pages with it
- no_rest_route error on custom routes
- WP_REST_Response – How to return Gzip answer and add Content-encoding header?
- REST api returns 404 on some WordPress sites
- Sending simple variable on single page to WP JSON
- Integrate admin plugin into template. Very interesant (live search + autocomplete with wp rest api, in vanilla js)
- Require()/Include() post template adding extra content
- Can’t pass var from php wp_ajax into ajax script : result undefined or null
- Force REST API Authentication for each request method
- How do i post data to url with fields?
- Call WP Rest-Api to GET /users/me returned NOTHING in console
- REST API custom endpoint to fetch pages and posts not working
- get Woocommerce product format json for WP_Query
- Issue with json decode in WordPress – value is not returning consistently
- How can i send id of the currently posted form data through json response in custom wordpress plugin?
- Need help to create WordPress page that decodes a text file and publishes it in JSON format
- How can i get the same ajax result using WP REST API instead of admin-ajax?
- WordPress PHP error getting posts from another wordpress blog
- Complex PHP for json_encode > how to handle/output right?
- Can’t get_users info by using json_encode
- AJAX Filter WHILE Loop not working WordPress
- I want to use wordpress database in android app using json encode?
- Result from wp_send_json adds line feed
- Setting pages for posts
- Search in particular category in wordpress
- WordPress format json in postmeta table
- How to post data to a word press site in case of a mobile app
- Pass a PHP variable (loop-audio.php) to jQuery function (js/script.js)
- I am unable to fetch json data from wp_db
- JSON Object Value Show Undefine
- Force array to be a string [closed]
- get_body_params() is always empty in POST request
- Headers already sent error with get_template_part in REST API call
- Properly process a custom WP REST API request (Authenticate, Authorize + Validate)?
- rest_cannot_create_user – Sorry, you are not allowed to create new users. CURL WORDPRESS REST API
- Query posts by custom taxonomy slug in WP REST API?
- oneOf JSON Schema validation not properly working for custom post meta value?
- Custom Navigation Bar JSON Syntax Error in JSON at Position 0
- Authenticate + Authorize WP REST API request before built-in WP JSON Schema Payload Validation?
- Adding Microsoft Teams Incoming Webhook to WordPress, Problem with Rest Route?
- WordPress REST API – Custom field not added to pages
- WordPress Register Block with metadata
- What is different about using admin-ajax.php in an endpoint and just submitting a form from the front end?
- REST API Plugin Update call back not updating the plugin
- regsiter a new user through WordPress API with Google SSO doesn’t work properly
- Woocommerce – how can i add items to cart using onclick – multiple items at once with custom prices
- I have issue with decoding json ex. ‘{“a”:”b”}’
- Registered a REST Route but I’m Getting a 400 Bad Request
- WP_Error Object in wp_remote_post & wp_remote_get
- Custom rest fields not loaded in rest api cpt response