如何在 wordpress 自定义端点中调用 class 方法作为回调函数?
How to call a class method as a callback function in wordpress custom endpoint?
我有一个自定义端点,如下所示:
add_action( 'rest_api_init', function () {
register_rest_route( 't2mchat/v2', '/get_curr_user_lang', array(
'methods' => 'GET',
'callback' => 'get_user_lang'
));
});
当它不是基于 class 的方法时,我能够调用回调函数 "get_user_lang"。但是一旦我将它转换为基于 class 的方法,我就无法调用它了。
我的 class 看起来像这样:
<?php
namespace T2mchat\TokenHandler;
class TokenHandler {
function get_user_lang() {
return "client_langs";
}
}
?>
我的新端点如下所示:
$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')
));
});
有人知道如何在 WordPress Rest API 自定义端点中调用基于 class 的方法吗?
Class WordPress Hooks 中的方法应该通过二维数组设置。
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')
));
});
如果钩子在 class if-self 中被调用并且你的回调方法在那里被定义:
add_action( 'rest_api_init', function () {
register_rest_route( 't2mchat/v2', '/get_curr_user_lang', array(
'methods' => 'GET',
'callback' => array($this,'get_user_lang')
));
});
如果来自不同 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')
));
});
如果此解决方案不起作用,您的问题的更多详细信息将有助于定义。
如果您使用相同 class 的静态方法,array() 解决方案将不起作用。我不得不使用 "magic" 常量 __CLASS__
:
'callback' => __CLASS__ . '::get_posts',
并且要使用此自定义 class 添加一个动作,并将另一个静态方法作为回调,我必须使用此表示法:
require_once( get_template_directory() . "/inc/classes/Rest/Posts.php" );
add_action( 'rest_api_init', 'Custom\Namespace\Posts::register_routes');
我有一个自定义端点,如下所示:
add_action( 'rest_api_init', function () {
register_rest_route( 't2mchat/v2', '/get_curr_user_lang', array(
'methods' => 'GET',
'callback' => 'get_user_lang'
));
});
当它不是基于 class 的方法时,我能够调用回调函数 "get_user_lang"。但是一旦我将它转换为基于 class 的方法,我就无法调用它了。
我的 class 看起来像这样:
<?php
namespace T2mchat\TokenHandler;
class TokenHandler {
function get_user_lang() {
return "client_langs";
}
}
?>
我的新端点如下所示:
$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')
));
});
有人知道如何在 WordPress Rest API 自定义端点中调用基于 class 的方法吗?
Class WordPress Hooks 中的方法应该通过二维数组设置。
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')
));
});
如果钩子在 class if-self 中被调用并且你的回调方法在那里被定义:
add_action( 'rest_api_init', function () {
register_rest_route( 't2mchat/v2', '/get_curr_user_lang', array(
'methods' => 'GET',
'callback' => array($this,'get_user_lang')
));
});
如果来自不同 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')
));
});
如果此解决方案不起作用,您的问题的更多详细信息将有助于定义。
如果您使用相同 class 的静态方法,array() 解决方案将不起作用。我不得不使用 "magic" 常量 __CLASS__
:
'callback' => __CLASS__ . '::get_posts',
并且要使用此自定义 class 添加一个动作,并将另一个静态方法作为回调,我必须使用此表示法:
require_once( get_template_directory() . "/inc/classes/Rest/Posts.php" );
add_action( 'rest_api_init', 'Custom\Namespace\Posts::register_routes');