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
- Ajax call to my WordPress website from an external application [duplicate]
- Buddypress update user avatar image via REST
- Delete taxonomy and delete all post related it
- Building a REST API for your web app exposes primary keys of DB records?
- How to set a template with wp_insert_post
- Permission callback to check if user has application password
- Multiple requests external data api dynamic block gutenberg
- 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
- WC_Customer delete function returns error
- Custom route and query
- Validate and Sanitize WP REST API Request using WP JSON Schema?
- entire JS folder not loading in a WP theme
- Filter by field with array value in ACF on WP REST API
- REST API works in browser and via AJAX but fails via cURL
- Retrieve Json data and create multiple pages with it
- PHP error with a shortcode: “no suitable wrapper” for file_get_contents
- no_rest_route error on custom routes
- Flatten Responses returned via WP REST API via WP_Error for obfuscation
- WP_REST_Response – How to return Gzip answer and add Content-encoding header?
- WordPress api post image raw data without being blank in media library
- 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
- WordPress 5.6 Application Passwords
- 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
- WooCommerce REST API AJAX Auth – 401 response
- Looping through and combining calls to Woocommerce REST API
- get Woocommerce product format json for WP_Query
- Issue with json decode in WordPress – value is not returning consistently
- How does one make a URL return dynamic JSON with custom Content Type?
- Need to forward Data from WooCommerce Webhook sent to same site WordPress REST API custom endpoint
- 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
- Require advice handling a URL redirect from a Third Party. URL Params need to populate and then forward to payment
- 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
- wp_send_json erase last value
- 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)?
- How to pass and validate nonce in custom REST routes
- rest_cannot_create_user – Sorry, you are not allowed to create new users. CURL WORDPRESS REST API
- How to enqueue structured data as file – jsonld file seems not to work
- 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
- How to pass php array to external js file in WordPress
- Application password header not checked in REST API
- Woocommerce – how can i add items to cart using onclick – multiple items at once with custom prices
- register_rest_route, Compare purchase codes and send a response
- Can external API content be added to Rest API in WordPress
- Authorization and execution of the link
- 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
- WP rest api and litespeed incompatibility
- Issues applying JSON body filters in EU F&T Portal SEARCH API with WordPress integration
- Custom rest fields not loaded in rest api cpt response
- REST API request between domain and subdomain