been having this issue now for a while.

{"code":"rest_no_route","message":"No route was found matching the URL and request method","data":{"status":404}}

This is my PHP snippet

add_action( 'rest_api_init', function () {   
   $namespace = 'contact';
   $endpoint = '/conversation/(?P<id>)';
   register_rest_route( $namespace, $endpoint, array(
      'methods' => 'GET',
      'callback' => 'conversations',
   ));   
});

function conversations( $data ) {
    $return = array(
        'data'=> $data['id']
    );
    wp_send_json($return);
}

And I am submitting a form via front end.

<form method="GET" class="form inline" action="<?php echo(get_home_url() . '/wp-json/contact/conversation/'.$value->unique_id); ?>">
  <button value="submit" class="ui green basic button reply" style="line-height: 0;">Reply</button>
</form>

It compiles to

<form method="GET" class="form inline" action="http://localhost:8888/wp-json/contact/conversation/304eb9aca04bae9d9b9d946968a4435c">
  <button value="submit" class="ui green basic button reply" style="line-height: 0;">Reply</button>
</form>

and the final url
http://localhost:8888/wp-json/contact/conversation/304eb9aca04bae9d9b9d946968a4435c?

As soon as I remove the ID from the url and form, it works… but I need the id as a parameter.

Read more here: WordPress REST API Custom Endpoints, not working with ID parameters


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: