[英]Wordpress REST API Custom Endpoints, not working with ID parameters
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 这是我的PHP代码段
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?
以及最终网址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. 只要我从网址和表单中删除ID,它就可以工作...但是我需要将ID作为参数。
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 我使用(?P<id>[a-zA-Z0-9-]+)
而不是(?P<id>)
来匹配任何字符串
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.