9

I have a custom endpoint which looks like this:

add_action( 'rest_api_init', function () {
    register_rest_route( 't2mchat/v2', '/get_curr_user_lang', array(
        'methods' => 'GET',
        'callback' => 'get_user_lang'
    ));
});

I was able to call the callback function "get_user_lang" when it wasn't a class based method. But once I converted it to a class based method, I wasn't able to call it.

My class looks like this:

<?php
namespace T2mchat\TokenHandler;


class TokenHandler {
  function get_user_lang() {
    return "client_langs";
  }
}
?>

and my new endpoint looks like this:

$t2m = new T2mchat\TokenHandler\TokenHandler();
add_action( 'rest_api_init', function () {
    register_rest_route( 't2mchat/v2', '/get_curr_user_lang', array(
        'methods' => 'GET',
        'callback' => array($t2m, 'get_user_lang')
    ));
});

Anyone have any idea on how to call a class based method in WordPress Rest API custom endpoints?

3 Answers 3

23

If the hook is called within the class if-self and yout callback method is defined there:

add_action( 'rest_api_init', function () {
    register_rest_route( 't2mchat/v2', '/get_curr_user_lang', array(
        'methods' => 'GET',
        'callback' => array($this,'get_user_lang')
    ));
});

If from different class:

add_action( 'rest_api_init', function () {
    register_rest_route( 't2mchat/v2', '/get_curr_user_lang', array(
        'methods' => 'GET',
        'callback' => array(new className,'get_user_lang')
    ));
});

If this solution is not working, a bit more details of your problem will help in defining.

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

2 Comments

the method is in a different class and i'm not using class based file where my hook is located. please see updated question and let me know if you are still not clear about it @ahmed-maruf
using the hook within the class itself solved my issue, thanks.
3

If you're using a static method of the same class, the array() solution doesn't work. I had to use the "magic" constant __CLASS__ :

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

And to add an action using this custom class with another static method as callback I had to use this notation:

require_once( get_template_directory() . "/inc/classes/Rest/Posts.php" );
add_action( 'rest_api_init', 'Custom\Namespace\Posts::register_routes');

3 Comments

I am trying a similar approach, what do I do if my callback is in a different class? 'callback' => 'OtherClass::SomeMethod' ?
Here is a Github-Gist as the comments here are not formatted: gist.github.com/Tech-Nomad/d3f28cf3da52f5d343de2e37dd992ed5
Oh, and if the needed class was already included/required before by a plugin or the theme, then you don't need to require the file as done on the first line in the gist.
1

Class methods in WordPress Hooks should be set via 2 dimensional array.

add_action( 'rest_api_init', function () {
    register_rest_route( 't2mchat/v2', '/get_curr_user_lang', array(
        'methods' => 'GET',
        'callback' => array($class_object,'get_user_lang')
    ));
});

1 Comment

{"code":"rest_invalid_handler","message":"The handler for the route is invalid","data":{"status":500}} --> the error i am getting by using your solution

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.