rest_no_route on custom API endpoint wordpress

I’ve been trying to figure this out for the last day or so to no avail.

I’m trying to create an endpoint to gather data from a set of tables. I’m trying to write it in OOP because I need to learn.

here’s my class:

class b2_items_rest_route extends WP_REST_Controller{
private $wpdb;

public function __construct(){
global $wpdb;
$this->wpdb = $wpdb;
}

public function b2_register_route(){
$version = ‘1’;
$namespace = ‘b2rest/v’ . $version;
$base = ‘route’;

register_rest_route( $namespace, ‘/’ . $base,
array(
array(
‘methods’ => WP_REST_Server::READABLE,
‘callback’ => array($this, ‘items_by_id’),
‘permission_callback’ => array($this, ‘get_items_permissions_check’ ),
‘args’ => array(),
),
)
);

register_rest_route( $namespace, ‘/’ . $base . ‘/trees/(?P<id>[d]+)’,
array(
array(
‘methods’ => WP_REST_Server::READABLE,
‘callback’ => array($this, ‘items_by_id’),
‘permission_callback’ => array($this, ‘get_item_permissions_check’ ),
‘args’ => array(),
)
)
);
}

function items_by_id($request){
global $wpdb;
//$tree_id = $request->get_param(‘id’);
$tree = $wpdb->get_results(“SELECT * FROM bb_bower__tree_configuration WHERE tree_id=47”);
}

public function get_items_permissions_check($request){
return true; // make endpoint available for everyone to read.
}

}

$b2_items_rest_route = new b2_items_rest_route;
add_action(‘rest_api_init’, $b2_items_rest_route->b2_register_route);

the problem is that I keep getting rest_no_route in postman but when I re-write it regularly it works fine.

any direction would be greatly appreciated, thanks!

Read more here:: rest_no_route on custom API endpoint wordpress

Leave a Reply

Your email address will not be published. Required fields are marked *