admin管理员组文章数量:1130349
Does WordPress have a core function that will check if a rest namespace exists?
I would like to use the following function (or something similar) in multiple plugins so it would be helpful if there was something like this available in core.
function rest_namespace_exists( string $namespace ) {
// Create a rest request for the current rest url of the site
$request = \WP_REST_Request::from_url( get_rest_url( null, '' ) );
$response = rest_do_request( $request );
$server = rest_get_server();
$data = $server->response_to_data($response, false);
return array_search( $namespace, $data['namespaces'] ) !== false;
}
I'm struggling because:
If I duplicate the code it's not DRY
If I use the above functions in one plugin then I instantly couple the two plugins and have to use both even though I may only want one (depending on each site's requirements).
If I build a 'Helper' plugin then I have to ask users who may be using the public plugin to download an additional plugin. Plus I don't know how comfortable I'd feel installing a XYZ Helpers that could have any number of unwanted functions/bloat in it.
Does WordPress have a core function that will check if a rest namespace exists?
I would like to use the following function (or something similar) in multiple plugins so it would be helpful if there was something like this available in core.
function rest_namespace_exists( string $namespace ) {
// Create a rest request for the current rest url of the site
$request = \WP_REST_Request::from_url( get_rest_url( null, '' ) );
$response = rest_do_request( $request );
$server = rest_get_server();
$data = $server->response_to_data($response, false);
return array_search( $namespace, $data['namespaces'] ) !== false;
}
I'm struggling because:
If I duplicate the code it's not DRY
If I use the above functions in one plugin then I instantly couple the two plugins and have to use both even though I may only want one (depending on each site's requirements).
If I build a 'Helper' plugin then I have to ask users who may be using the public plugin to download an additional plugin. Plus I don't know how comfortable I'd feel installing a XYZ Helpers that could have any number of unwanted functions/bloat in it.
1 Answer
Reset to default 2You can use the WP_REST_Server::get_namespaces() method.
For instance.
$exists = in_array( 'ns/v1', rest_get_server()->get_namespaces() );
Does WordPress have a core function that will check if a rest namespace exists?
I would like to use the following function (or something similar) in multiple plugins so it would be helpful if there was something like this available in core.
function rest_namespace_exists( string $namespace ) {
// Create a rest request for the current rest url of the site
$request = \WP_REST_Request::from_url( get_rest_url( null, '' ) );
$response = rest_do_request( $request );
$server = rest_get_server();
$data = $server->response_to_data($response, false);
return array_search( $namespace, $data['namespaces'] ) !== false;
}
I'm struggling because:
If I duplicate the code it's not DRY
If I use the above functions in one plugin then I instantly couple the two plugins and have to use both even though I may only want one (depending on each site's requirements).
If I build a 'Helper' plugin then I have to ask users who may be using the public plugin to download an additional plugin. Plus I don't know how comfortable I'd feel installing a XYZ Helpers that could have any number of unwanted functions/bloat in it.
Does WordPress have a core function that will check if a rest namespace exists?
I would like to use the following function (or something similar) in multiple plugins so it would be helpful if there was something like this available in core.
function rest_namespace_exists( string $namespace ) {
// Create a rest request for the current rest url of the site
$request = \WP_REST_Request::from_url( get_rest_url( null, '' ) );
$response = rest_do_request( $request );
$server = rest_get_server();
$data = $server->response_to_data($response, false);
return array_search( $namespace, $data['namespaces'] ) !== false;
}
I'm struggling because:
If I duplicate the code it's not DRY
If I use the above functions in one plugin then I instantly couple the two plugins and have to use both even though I may only want one (depending on each site's requirements).
If I build a 'Helper' plugin then I have to ask users who may be using the public plugin to download an additional plugin. Plus I don't know how comfortable I'd feel installing a XYZ Helpers that could have any number of unwanted functions/bloat in it.
1 Answer
Reset to default 2You can use the WP_REST_Server::get_namespaces() method.
For instance.
$exists = in_array( 'ns/v1', rest_get_server()->get_namespaces() );
本文标签: wp apiCore function to check if a rest namespace exists
版权声明:本文标题:wp api - Core function to check if a rest namespace exists 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749216659a2334273.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论