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

Verify nonce in REST API?

You should pass the special wp_rest nonce as part of the request. Without it, the global $current_user object will not be available in your REST class. You can pass this from several ways, from $_GET to $_POST to headers. The action nonce is optional. If you add it, you can’t use the REST endpoint from … Read more

Edit srcset and sizes attributes in Gutenberg image, cover and gallery – blocks

Try the suggestion below: This assumes the <figure> element holding an image has a data-display-alignment attribute to match the alignment class applied attached that is somehow surfaced as a parameter in the wp_calculate_image_sizes hook: /** * Add custom image sizes attribute to enhance responsive image functionality * for content images. * * @param string $sizes … Read more

WP REST API create post authentication issue

This is an old question, but the current REST API indicates that the X-WP-NONCE header should be set via the beforeSend callback. The following is directly from the REST API docs. .ajax( { url: wpApiSettings.root + ‘wp/v2/posts/1’, method: ‘POST’, beforeSend: function ( xhr ) { xhr.setRequestHeader( ‘X-WP-Nonce’, wpApiSettings.nonce ); }, data:{ ‘title’ : ‘Hello Moon’ … Read more

WP REST API V2 – Retrieve sub page by full slug (URL/Path)

Unfortunately, this functionality is not natively supported out of the box. In detail, the problem is that most post types including page use the base WP_REST_Posts_Controller which maps the slug parameter to the post_name__in WP_Query argument, which does not facilitate resolving hierarchical slugs. The pagename query variable does, however – but only one per query, … Read more

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