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 badges
Add a comment  | 

1 Answer 1

Reset to default 1

Is 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