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

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

How to use _embed when using _fields?

It is not clear in the documentation, but you need to include the “_links” and “_embedded” as fields to be returned. In addition, I include the _embed parameter, as it does not require a value. As of WordPress 5.4, the resources to embed can be limited by passing a list of link relation names to … Read more

Getting user meta data from WP REST API

Look into register_rest_field() to register meta with the rest api. add_action( ‘rest_api_init’, ‘adding_user_meta_rest’ ); function adding_user_meta_rest() { register_rest_field( ‘user’, ‘collapsed_widgets’, array( ‘get_callback’ => ‘user_meta_callback’, ‘update_callback’ => null, ‘schema’ => null, ) ); } And then put your get_user_meta bit in the callback. function user_meta_callback( $user, $field_name, $request) { return get_user_meta( $user[ ‘id’ ], $field_name, true … Read more

Displaying a page built with Elementor using the REST API [closed]

you can create a new endpoint with the following code and you can retrieve the elementor content on URL wp-json/MyPlugin/v1/pages/PAGE_ID/contentElementor add_action(“rest_api_init”, function () { register_rest_route( “MyPlugin/v1” , “/pages/(?P<id>\d+)/contentElementor” , [ “methods” => “GET”, “callback” => function (\WP_REST_Request $req) { $contentElementor = “”; if (class_exists(“\\Elementor\\Plugin”)) { $post_ID = $req->get_param(“id”); $pluginElementor = \Elementor\Plugin::instance(); $contentElementor = $pluginElementor->frontend->get_builder_content($post_ID); } … Read more

WP REST API Require Password for GET Endpoint

When we register a rest route with register_rest_route(), then we can use the permission_callback parameter with the kind of permission we want. Check for example how WP_REST_Posts_Controller::register_routes() and WP_REST_Users_Controller::register_routes() implement the permission callback. The password argument you’re referring to is the content’s password, that you can set for each post and that’s not the same. … Read more

How would I add custom tables/endpoints to the WP REST API?

I eventualy worked out a solution for my restaurants table, which sits alongside the wp_* tables in my WP database. Hope this helps add_action( ‘rest_api_init’, function () { register_rest_route( ‘restos/v1’, ‘/all’, array( ‘methods’ => ‘GET’, ‘callback’ => ‘handle_get_all’, ‘permission_callback’ => function () { return current_user_can( ‘edit_others_posts’ ); } ) ); } ); function handle_get_all( $data … Read more

check the requesting url

That filter is definitely not the one you are looking for. That filter fires before returning the result of WP_REST_Request::from_url() which appears to be a factory method that is only used internally to handle embeds. A better option is to return a WP_Error instance on the rest_pre_dispatch filter. Some caveats: As mentioned by @milo, the … Read more

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