0
votes

This is my Custom End point but its says "No route was found matching the URL and request method" i added in the theme folder function.php file. when i changed the method 'POST' to 'GET' it works fine for get method .htaccess file is ok any other plugin like securty or other rest api plugin not installed only "WP REST API plugin" is activated

add_action( 'rest_api_init', 'myfunction' );
function myfunction() {
register_rest_route( 'app', '/addmeta', array(
    'methods'  => 'POST',
    'callback' => 'vendor_serve_route'
) );
}

function vendor_serve_route(WP_REST_Request $request_data ) {
// Fetching values from API
$data = $request_data->get_params();`enter code here`
$user_data = array( 'user_login'     => $data['first_name'],
                    'user_email'     => $data['user_email'],
                    'nickname'       => $data['user_name'],
                    'first_name'     => $data['first_name'],
                    'last_name'      => $data['last_name'],

                    );


 return ['Data' => $user_data];


 }
1
The code seems fine to create an endpoint (although I would advise you to create a plugin just for this), so it should be an error in your request for the endpoint. Are you also changing your request to HTTP's POST method? You have to change it in the endpoint and wherever you're making the request.gd_silva
Thanks.You are right .The problem is HTTPSmartBird
@SmartBird I tried your example .... but it failed :(user10090131

1 Answers

1
votes

Already stated in the comments but now with a little bit more explanation:

The code seems fine to create an endpoint, so it should be an error in your request for the endpoint. The problem is that you're just changing the endpoint's HTTP method but not the HTTP method of the actual request to the endpoint.

This code will create an endpoint that accepts POST requests to YOUR_DOMAIN/wp-json/app/addmeta.

It's not a good practice to add a lot of code to the functions.php file, as it will get really big and hard to maintain.

You should create a plugin (it's a really simple process) for this and put the new code there. This way, it's isolated and you can even reuse it in other apps easily.

To create a plugin you need to:

  • Create a new directory under /plugins for your plugin
  • Create a .php file in that directory with the same name as the directory
  • Include a header comment in that main file that describes your plugin

Example:

  • Directory: .../plugins/my-api-endpoints
  • File: my-api-endpoints.php

And then include the comment in the file:

<?php
/**
 * Plugin Name: My API Endpoints
 * Plugin URI: 
 * Description: This plugins handles the submissions for my API.
 * Version: 0.1
 * Author: John Doe
 * Author URI: http://johndoe
 */

your code here...

Read this and this for more info about creating plugins. It'll help you getting started.