WordPress Plugin Development: How to Create an API Call without Auth

106 Views Asked by At

This My WordPress Plugin API code

add_action('rest_api_init', function () {
    register_rest_route(BG_API_BASE_URL, 'chapters/(?P<chapter_id>[a-zA-Z0-9-]+)/lession', [
        'methods' => 'GET',
        'callback' => 'bg_lession',
    ]);

});

function bg_lession(WP_REST_Request $request)
{

    global $wpdb;
    $chapter_id = (int) $request->get_param('chapter_id');
    $chapters_table_name = BG_LESSION_TABLE;
    $result = $wpdb->get_results("SELECT * FROM $chapters_table_name WHERE chapter_id = $chapter_id");
    return $result;
} 

Issue of Response

{
    "success": false,
    "data": [
        {
            "code": "rest_unauthorized",
            "message": "Unauthorized"
        }
    ]
}

How To Call API without Auth ? It is working on WP Admin Login in Brower CAll API

0

There are 0 best solutions below