WP Rest API: new route return rest_invalid_handler

Viewed 7640

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

4 Answers

Solved!

'callback' => __NAMESPACE__ . '\\testing_route',

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),

Try this :

'callback' => __CLASS__ . '::testing_route',

Your callback shouldn't take any args, just remove $data

Related