Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WP Rest API: new route return rest_invalid_handler

This is a simple test script for create a new route:

add_action( 'rest_api_init', function () {
  register_rest_route( 'ass', '/ativar', array(
    'methods' => 'GET',
    'callback' => 'testing_route',
  ) );
} );

function testing_route($data){
    return array( 'message' => 'testing route' );
}

But it returning an error message:

rest_invalid_handler

like image 601
marcelo2605 Avatar asked Nov 28 '22 13:11

marcelo2605


2 Answers

Solved!

'callback' => __NAMESPACE__ . '\\testing_route',
like image 112
marcelo2605 Avatar answered Dec 01 '22 03:12

marcelo2605


In my case I was setting up register_rest_route within a function inside of my class. I simply added:

'callback' => array($this, 'name_of_callback_function),
like image 43
coletrain Avatar answered Dec 01 '22 01:12

coletrain