marcelo2605
marcelo2605

Reputation: 2794

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

Upvotes: 7

Views: 10764

Answers (4)

coletrain
coletrain

Reputation: 2849

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

Upvotes: 14

Hendra
Hendra

Reputation: 81

Try this :

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

Upvotes: 5

marcelo2605
marcelo2605

Reputation: 2794

Solved!

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

Upvotes: 16

rak007
rak007

Reputation: 1011

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

Upvotes: 1

Related Questions