Authenticate current user to REST API

I solved my problem. This got me on the right track: https://developer.wordpress.org/rest-api/using-the-rest-api/authentication/ You can just make an api call via frontend and jquery. $.ajax( { url: ‘<?php echo esc_url_raw( rest_url()) ?>’ + ‘your/endpoint’, method: ‘GET’, beforeSend: function ( xhr ) { xhr.setRequestHeader( ‘X-WP-Nonce’, ‘<?php echo wp_create_nonce( ‘wp_rest’)?>’ ); } } ).done( function ( response ) … Read more

Filter DELETE REST API calls

pre_delete_post hook filters whether a post deletion should take place. So callback function must return a boolean value: true – whether to go forward with deletion, false – if not. pre_trash_post hook filters whether a post trashing should take place. So callback function must return a boolean value: true – whether to go forward with … Read more

Setting ?context=edit results in rest_forbidden_context, even for an Administrator user

Clearly your administrator is unable to modify those posts according to the roles and capabilities system. This is the code that is generating that error in WP_REST_Post_Types_Controller: public function get_items_permissions_check( $request ) { if ( ‘edit’ === $request[‘context’] ) { $types = get_post_types( array( ‘show_in_rest’ => true ), ‘objects’ ); foreach ( $types as $type … Read more

WordPress REST API V2: how to get list of all posts?

Out of the box and using the core available hooks and API, you can’t have more than 100 items per response on WordPress REST API for performance reasons. For the second part of the question, you may remove some fields from the response by using _fields parameter in your request as you can see in … Read more

WP_REST_Response() doesn’t seem to return the expected object

I believe WP_Rest_Response did return the proper response based on what your endpoint callback returns, but I think you just not understanding what fetch() returns, so please check the documentation on MDN for more details, but here’s an excerpt that might help you: The fetch() method takes one mandatory argument, the path to the resource … Read more

How to get featured image in WP rest api

You can add featured image to REST API by adding this code to your theme’s functions.php // Add featured image to REST API add_action(‘rest_api_init’, ‘register_rest_images’ ); function register_rest_images(){ register_rest_field( array(‘post’), ‘fimg_url’, array( ‘get_callback’ => ‘get_rest_featured_image’, ‘update_callback’ => null, ‘schema’ => null, ) ); } function get_rest_featured_image( $object, $field_name, $request ) { if( $object[‘featured_media’] ){ $img … Read more

How to store and return json in a (custom) post meta field

I’m not exactly sure what are you really trying to do, but anyway, I hope these help? If you send the meta value as an object, e.g. video: {“foo”:”bla blah”,”bar”:1234} in JS, then note that WordPress will serialize the value (or convert the object to a serialized string) before it’s saved to the database. See … Read more

If I use WordPress REST API V2 and someone makes an app using it. Will my site count the posts views from the APP? And if not, then how?

WordPress does not count views or traffic, it’s not a part of WordPress’ feature set. Plugins and code snippets can add this but it’s highly specific to the service you’re using. Usually for apps you will need to integrate tracking into the app itself. But how you would do that is specific to the service … Read more

Error invalid parameters with REST API

The error in question — “Invalid parameter(s): tags” and “tags[0] is not of type integer.“, means that you need to supply a list of tag IDs and not names or slugs. So for examples, ‘tags’ => 123 and ‘tags’ => [ 123 ] are both valid. (Comma-separated list is also accepted, e.g. ‘tags’ => ‘123,4,5’.) … Read more

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