admin管理员组文章数量:1296220
I cannot find any info on this. Is there a default endpoint for sidebars?
For example pages:
http://localhost:8888/example/wp-json/wp/v2/pages
I have looked at the WordPress API documentation but don't see any endpoints for sidebars or widgets.
I also tried something basic like this:
function sidebar_api( $data ) {
$response_body = get_sidebar('one');
return new WP_REST_Response(
array(
'body_response' => $response_body
)
);
}
add_action( 'rest_api_init', function () {
register_rest_route( 'custom/v1', 'sidebar1', array(
'methods' => 'GET',
'callback' => 'sidebar_api',
) );
} );
which gives me the sidebar not in json format and then a json response with an empty body_response.
I cannot find any info on this. Is there a default endpoint for sidebars?
For example pages:
http://localhost:8888/example/wp-json/wp/v2/pages
I have looked at the WordPress API documentation but don't see any endpoints for sidebars or widgets.
I also tried something basic like this:
function sidebar_api( $data ) {
$response_body = get_sidebar('one');
return new WP_REST_Response(
array(
'body_response' => $response_body
)
);
}
add_action( 'rest_api_init', function () {
register_rest_route( 'custom/v1', 'sidebar1', array(
'methods' => 'GET',
'callback' => 'sidebar_api',
) );
} );
which gives me the sidebar not in json format and then a json response with an empty body_response.
Share Improve this question edited Apr 4, 2021 at 7:07 user8463989 asked Jan 12, 2021 at 6:42 user8463989user8463989 5931 gold badge8 silver badges24 bronze badges1 Answer
Reset to default 1Is there a default endpoint for sidebars?
No, I don't think there is. So (for now), using a custom REST API endpoint does sound like a good option to me.
gives me the sidebar not in json format and then a json response with an empty
body_response
That's because get_sidebar()
echo the output, hence the $response_body
is empty.
So you'd want to use output buffering like so:
ob_start();
get_sidebar( 'one' );
$response_body = ob_get_clean();
本文标签: rest apiSidebar endpoint using WordPress API
版权声明:本文标题:rest api - Sidebar endpoint using WordPress API 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741635922a2389638.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论