How can I get users email (and additional data) from the rest API?

To add the user’s email address in the REST API response, register an additional field to the user object and add the email address: register_rest_field( ‘user’, ‘user_email’, [ ‘get_callback’ => static function (array $user): string { return get_userdata($user[‘id’])->user_email; }, ] ); Please note, however, that this is strongly discouraged because anyone can then see the … Read more

REST API endpoint for elasticpress autosuggest

After some inspecting the WP_REST_Request, it turned out, that the get_body() method was the one I’m looking for. Anyhow, this is what I ended up with: add_action( ‘rest_api_init’, function() { register_rest_route( ‘ep’, ‘/as/’, [ ‘methods’ => \WP_REST_Server::CREATABLE, ‘callback’ => ‘ep_autosuggest’, ] ); } ); function ep_autosuggest( WP_REST_Request $data ) { // Elasticsearch PHP Client $client … Read more

Why is my custom API endpoint not working?

Maybe start with just GET. Your route looks weird as well. Try just: register_rest_route(‘my-project/v1’, ‘/action/’, [ ‘methods’ => WP_REST_Server::READABLE, ‘callback’ => ‘api_method’, ]); And your callback is not returning a valid response. Let your callback look more like this: $data = [ ‘foo’ => ‘bar’ ]; $response = new WP_REST_Response($data, 200); // Set headers. $response->set_headers([ … Read more

In Gutenberg, now that withAPIData is being deprecated, how can I do an async call to a custom endpoint?

It’s worth noting that the answer above is out of date. The data store should now look like this: const actions = { setUserRoles( userRoles ) { return { type: ‘SET_USER_ROLES’, userRoles, }; }, receiveUserRoles( path ) { return { type: ‘RECEIVE_USER_ROLES’, path, }; }, }; const store = registerStore( ‘matt-watson/secure-block’, { reducer( state = … Read more

Dynamic Endpoints

10up engineering best practices as a great example on how to achieve that. You basically add a new rewrite tag and a new rewrite rule and then using the template_redirect action you return your custom action. Here is the code: add_action( ‘init’, function() { add_rewrite_tag( ‘%model%’, ‘([^/]+)’ ); add_rewrite_rule( ‘cars/([^/]+)/?’, ‘index.php?model=$matches[1]’, ‘top’ ); } ); … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)