WP Rest API: new route return rest_invalid_handler
Asked Answered
H

4

7

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

Hornbill answered 21/9, 2017 at 10:18 Comment(0)
H
15

Solved!

'callback' => __NAMESPACE__ . '\\testing_route',
Hornbill answered 21/9, 2017 at 10:26 Comment(3)
Edit this so it becomes the answer of your questionRudich
nice, forgot to concatenate the namespace :)Manikin
Also note, if you are using a object method as a callback, it needs to have public visibility, else it will return nothing.Gyrostatics
E
13

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'),
Eason answered 15/9, 2019 at 2:6 Comment(0)
D
5

Try this :

'callback' => __CLASS__ . '::testing_route',
Dislocate answered 22/6, 2018 at 14:22 Comment(2)
You should specif an answer more extends and with more detailsHitherward
Solved my issue when using class methods vs instance methods. Ripper!Cousingerman
R
1

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

Rudich answered 21/9, 2017 at 10:21 Comment(1)
actually, the callbacks need this argument in case they have to do anything with the data sent in the requestsFini

© 2022 - 2024 — McMap. All rights reserved.