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

Rest API authentication issue when called from fetch request in bundle.js

The problem is that you’re not using the correct name for the REST API nonce header — the correct name is X-WP-Nonce, but you used X-WP-Header: fetch(‘http://localhost/wptest2/?rest_route=/wp/v2/users/me’, { method : ‘get’, mode : ‘cors’, headers : { ‘Access-Control-Allow-Origin’ : ‘*’, ‘X-WP-Header’ : _wpnonce // here you used the wrong name } }) Reference from the … Read more

Can we access the REST request parameters from within the permission_callback to enforce a 401 by returning false?

I’ve created a reduced test case that demonstrates that what you want to do is achievable: add_action( ‘rest_api_init’, function() { register_rest_route( ‘wpse/343039’, ‘route’, [ ‘methods’ => [ ‘POST’ ], ‘permission_callback’ => function( WP_REST_Request $request ) { if ( ‘1’ == $request->get_param( ‘param’ ) ) { return true; } else { return false; } }, ‘callback’ … Read more

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