2

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.

1 Answer 1

2

Basically, the error was that I did not declare regex correct.

instead of (?P<id>) i used (?P<id>[a-zA-Z0-9-]+) that way I was matching for any string

Sign up to request clarification or add additional context in comments.

1 Comment

This is old, but I think you could have used (?P<id>\w+) instead. (matching all characters). Plus, naming something "id" and expecting more than digits may be confusing. I recommand to use another name for your var.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.