Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 639 Vote(s) - 3.48 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to call a class method as a callback function in wordpress custom endpoint?

#1
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?
Reply

#2
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')
));
});
Reply

#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');
Reply

#4
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.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through