How can I expose rest API in custom plugin ? I want to expose this route, When I use it into functions.php, it works, but when I use it in my custom plugin, the route is not found.
add_action( 'rest_api_init', 'my_register_route');
function my_register_route() {
register_rest_route( 'my-route', 'my-posts', array(
'methods' => 'GET',
'callback' => 'my_posts',
'permission_callback' => function() {
return current_user_can( 'edit_others_posts' );
},
);
}