admin管理员组文章数量:1391987
I am attempting to start creating a custom endpoint for one of my sites, so I can pull in ACF fields to another. I have the following code in my theme's functions.php
// register the endpoints needed
add_action( 'rest_api_init', function( ) {
/*
// post types to include
$_pts = array( 'posts', );
// loop the post types and create a rest endpoint for the ACF fields for them
foreach ( $_pts as $_pt ) {
// var_dump($_pt . '/acf');
// register the rest endpoint
register_rest_route( 'kp-api/v1', $_pt . '/acf/(?P<id>\d+)', array(
'methods' => array( 'GET', ),
'get_callback' => function( $data ) {
return 'HERE';
},
'args' => array(
'id' => array( 'validate_callback' => function( $param, $request, $key ) {
return is_numeric( $param );
} ),
),
) );
}
*/
// register the rest endpoint
register_rest_route( 'kp-api/v1', '/TEST/(?P<id>\d+)', array(
'methods' => array( 'GET', ),
'get_callback' => function( $data ) {
return 'HERE';
},
'args' => array(
'id' => array( 'validate_callback' => function( $param, $request, $key ) {
return is_numeric( $param );
} ),
),
) );
} );
And when I try to browse my endpoint, I end up with {"code":"rest_no_route","message":"No route was found matching the URL and request method","data":{"status":404}}
And, I cannot see the endpoint in the site's /wp-json/. I can however, see that at least the namespace itself is indeed created by browsing to it: /wp-json/kp-api/v1
However, there are no endpoints in it.
{
"namespace": "kp-api/v1",
"routes": {
"/kp-api/v1": {
"namespace": "kp-api/v1",
"methods": [
"GET"
],
"endpoints": [
{
"methods": [
"GET"
],
"args": {
"namespace": {
"required": false,
"default": "kp-api/v1"
},
"context": {
"required": false,
"default": "view"
}
}
}
],
"_links": {
"self": ""
}
}
},
"_links": {
"up": [
{
"href": "/"
}
]
}
}
What am I doing wrong? No, I'm not using the wp-json plugin, just the normal built-in Wordpress functionality (v.5.3.2)
I am attempting to start creating a custom endpoint for one of my sites, so I can pull in ACF fields to another. I have the following code in my theme's functions.php
// register the endpoints needed
add_action( 'rest_api_init', function( ) {
/*
// post types to include
$_pts = array( 'posts', );
// loop the post types and create a rest endpoint for the ACF fields for them
foreach ( $_pts as $_pt ) {
// var_dump($_pt . '/acf');
// register the rest endpoint
register_rest_route( 'kp-api/v1', $_pt . '/acf/(?P<id>\d+)', array(
'methods' => array( 'GET', ),
'get_callback' => function( $data ) {
return 'HERE';
},
'args' => array(
'id' => array( 'validate_callback' => function( $param, $request, $key ) {
return is_numeric( $param );
} ),
),
) );
}
*/
// register the rest endpoint
register_rest_route( 'kp-api/v1', '/TEST/(?P<id>\d+)', array(
'methods' => array( 'GET', ),
'get_callback' => function( $data ) {
return 'HERE';
},
'args' => array(
'id' => array( 'validate_callback' => function( $param, $request, $key ) {
return is_numeric( $param );
} ),
),
) );
} );
And when I try to browse my endpoint, I end up with {"code":"rest_no_route","message":"No route was found matching the URL and request method","data":{"status":404}}
And, I cannot see the endpoint in the site's /wp-json/. I can however, see that at least the namespace itself is indeed created by browsing to it: /wp-json/kp-api/v1
However, there are no endpoints in it.
{
"namespace": "kp-api/v1",
"routes": {
"/kp-api/v1": {
"namespace": "kp-api/v1",
"methods": [
"GET"
],
"endpoints": [
{
"methods": [
"GET"
],
"args": {
"namespace": {
"required": false,
"default": "kp-api/v1"
},
"context": {
"required": false,
"default": "view"
}
}
}
],
"_links": {
"self": "https://example/wp-json/kp-api/v1"
}
}
},
"_links": {
"up": [
{
"href": "https://example/wp-json/"
}
]
}
}
What am I doing wrong? No, I'm not using the wp-json plugin, just the normal built-in Wordpress functionality (v.5.3.2)
Share Improve this question edited Mar 9, 2020 at 13:11 Kevin asked Mar 9, 2020 at 13:01 KevinKevin 1812 silver badges9 bronze badges1 Answer
Reset to default 0You’re using the wrong function. It’s register_rest_route()
not register_rest_field()
.
本文标签: rest apiWordpress API quotcodequotquotrestnoroutequot with Custom Route
版权声明:本文标题:rest api - Wordpress API "code":"rest_no_route" with Custom Route 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744684770a2619631.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论