Extend WordPress REST API to show slugs for menu items

I had the same challenge, and fixed it by adding it to the response. Example: function get_menu() { # Change ‘menu’ to your own navigation slug. $menu_items = wp_get_nav_menu_items(‘menu’); foreach($menu_items as $menu_item) { // ALTERNATIVE: $slug = get_post_field( ‘post_name’, $menu_item->object_id ); $slug = basename( get_permalink($menu_item->object_id) ); $menu_item->slug = $slug; } return $menu_items; } add_action( ‘rest_api_init’, … Read more

Unable to get the info of the user which doesn’t have created any post via REST API

This is by design and within WP_REST_Users_Controller::get_item_permissions_check() we e.g. have a non-zero check with count_user_posts( $user->ID, $types ). We note that count_user_posts() is filterable with the get_usernumposts filter so one could change 0 to 1, for the /wp/v2/users/\d+ route: add_filter( ‘rest_request_before_callbacks’, function( $response, $handler, $request ){ if ( WP_REST_Server::READABLE !== $request->get_method() ) { return $response; … Read more

rest api authentication

The WordPress core now supports a new REST API as of version 4.8.x. Among a sea of new possibilities, one can now build a front-end for a website or app with a framework like React or Angular and use WordPress and its familiar admin dashboard to manage the back-end. So pulling all the content in … Read more

WP rest api v2 posts filter by ‘search’ & ‘category_name’ not working

The filter parameter was removed as part of WP 4.7, to get the results you expect you’ll need to change http://examples.com/wp-json/wp/v2/posts?filter[category_name]=uncategorize&per_page=30&order=asc&search=hello to http://examples.com/wp-json/wp/v2/posts?categories=1&per_page=30&order=asc&search=hello Make sure to change categories=0 to whatever uncategorised‘s tag_ID is. If you’d like to restore the Filter parameter, I would recommend this plugin: https://github.com/WP-API/rest-filter

data returned from get_post($postId) have different keys than wp-json/wp/v2/posts/{postId}

You can set your own keys in the JSON response array. It can be anything. Take a look at this simple example: add_action( ‘rest_api_init’, function () { register_rest_route( ‘shubham’, ‘/get_the_post/’, array( ‘methods’ => ‘GET’, ‘callback’ => ‘get_posts_by_rest’ ) ); }); // Callback function function get_posts_by_rest( ){ // Get the post id from the URL and … Read more

register_rest_route regex option for base64 or alternate

I got curious, so I tested this barebone demo: add_action( ‘rest_api_init’, function () { register_rest_route( ‘wpse/v1’, ‘/post_by_permalink/(?P<path>[\S]+)’, [ ‘methods’ => WP_REST_Server::READABLE, ‘callback’ => ‘wpse_callback’, ‘show_in_rest’ => true ] ); }); function wpse_callback( $request ) { $data = [ ‘path’ => base64_decode( $request[‘path’] ) ]; return $data; } Testing: https://example.com/wp-json/wpse/v1/post_by_permalink/aHR0cHM6Ly93b3JkcHJlc3Muc3RhY2tleGNoYW5nZS5jb20vcS8zMDEwNjcv gives { path: “https://wordpress.stackexchange.com/q/301067/” } Tested … Read more