5

I'am developing a plugin for wordpress and have trouble with the Rest API. On my test server it works without a problem. (v4.6.6) On a different server (v4.4.10) the API returns this error message:

{"code":"rest_invalid_handler","message":"
Der Handler f\u00fcr die Route ist ung\u00fcltig","data":{"status":500}}%

The message is in german and means "The handler for the route is invalid." Don't understand why they translate the error messages for an API. Makes no sense for me. :)

The routes on the http://domain/wp-json are equal. Maybe an problem with the different WP versions?

Definition of the route:

function __construct() {
    add_action( 'rest_api_init', function(){
        register_rest_route( 'test_namespace', 'ping', array(
            'methods' => 'POST',
            'callback' => array($this, 'ping_test'),
            'permission_callback' =>  array($this, 'myhacks_permission_callback'),
        ) );
    } );
}

Thanks for help.

4
  • Can you show us the rest of the file? I believe the issue is coming from $this scope is wrong. Commented Sep 3, 2017 at 13:16
  • I think the problem is related to the PHP version. I get this error on all Servers with < PHP 5.4. That is ok for me. I updated the PHP version and now it's working. Commented Sep 5, 2017 at 12:40
  • 1
    $this is fine, mine come from using 'callback' => array($this => 'ping_test') instead of 'callback' => array($this, 'ping_test') Commented Sep 6, 2017 at 13:18
  • @Glen's comment did the trick for me! Commented Sep 12, 2019 at 17:47

2 Answers 2

12

I had the same issue. It seems that method ping_test cannot be private. If you change it to public, the error disappears.

Sign up to request clarification or add additional context in comments.

1 Comment

This answer helped me with the same issue. I have AjaxController class registered in FrontController. In the AjaxController I register REST routes in its constructor with 'callback' => [$this,'portfolioSave',], args array property of register_rest_route. My method was protected and it helped me when I changed it to public. Thanks a lot!
2

Take a look at the WordPress core and you can see that the method passed as the callback aka ping_test must be callable.

So this error triggers only when that method doesn't exist (for example I just encountered it because of a typo) or if is not accessible(like a protected or private method)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.