How to call a class method as a callback function in wordpress custom endpoint?
Asked Answered
D

3

8

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?

Delict answered 17/4, 2019 at 5:34 Comment(0)
R
22

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.

Responsive answered 17/4, 2019 at 7:46 Comment(2)
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-marufDelict
using the hook within the class itself solved my issue, thanks.Delict
M
2

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');
Margherita answered 19/8, 2019 at 14:7 Comment(3)
I am trying a similar approach, what do I do if my callback is in a different class? 'callback' => 'OtherClass::SomeMethod' ?Reproachful
Here is a Github-Gist as the comments here are not formatted: gist.github.com/Tech-Nomad/d3f28cf3da52f5d343de2e37dd992ed5Margherita
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.Margherita
V
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')
    ));
});
Vally answered 17/4, 2019 at 7:5 Comment(1)
{"code":"rest_invalid_handler","message":"The handler for the route is invalid","data":{"status":500}} --> the error i am getting by using your solutionDelict

© 2022 - 2024 — McMap. All rights reserved.