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]
- Problem with Woocommerce REST API Authentication
- API JSON Data in WordPress
- fetch_feed: retrieve entries in the appearing order, not chronologically
- 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?
- Continue execution after WP REST API response
- 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
- Upload a json file in php [closed]
- 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
- Create dynamic pages from external JSON data without storing in Database?
- Parsing php string in jquery [closed]
- How to filter posts by post format “standard” from wp-json api?
- woocommerce registration form with klaviyo(don’t work with current user)
- Why is the post meta[] empty when I make a call to the wordpress rest api?
- 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
- Woocommerce hook run after an Order been created through REST API
- Fetch post block/field data as JSON with WordPress API
- How to show single category archive
- Woocommerce api: create product with images – bad request
- Woocommerce: hook action/filter I could use to add variation id and price with each attribute opt on WooCommerce Rest api
- Are nonces in WP REST API optional by default?
- database sent to a 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
- REST API works in browser and via AJAX but fails via cURL
- 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?
- 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
- 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 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?
- WordPress REST API – Custom field not added to pages