У меня возникла проблема с созданием плагина WP, проблема в том, что я хочу использовать WP Rest API и расширять его с помощью собственной конечной точки.Зарегистрировать маршрут отдыха в библиотеке классов не работает
Я пишу код, используя объект класса, зарегистрировать add_action («rest_api_init» проблема у меня есть, что конечная точка теперь показывает в маршруте.
Вот код, когда я Инициализируем . плагин
class ThorAdmin {
public function __construct() {
// Activation and deactivation hook.
register_activation_hook(WP_PLUGIN_DIR . '/wp-thor-fcm/wp-thor-fcm.php', array($this, 'thor_fcm_activate'));
register_deactivation_hook(WP_PLUGIN_DIR . '/wp-thor-fcm/wp-thor-fcm.php', array($this, 'thor_fcm_deactivate'));
// Admin Menu
add_action('admin_menu', array($this, 'thor_admin_menu'));
add_action('admin_init', array($this, 'thor_fcm_settings_init'));
add_action('wpmu_new_blog', array($this, 'thor_on_new_blog', 10, 6));
add_action('activate_blog', array($this, 'thor_on_new_blog', 10, 6));
add_action('admin_enqueue_scripts', array($this, 'thor_head'));
//The Following registers an api route with multiple parameters.
add_action('rest_api_init', array($this, 'add_thor_fcm_route'));
add_filter('admin_footer_text', array($this, 'thor_fcm_admin_footer'));
}
Вот функции, которые я называю из add_action ('rest_api_init', массив ($ это, 'add_thor_fcm_route'));
/**
* Registers the routes for all and single options
*
*/
function add_thor_fcm_route() {
register_rest_route('wp/v2/thorfcmapi', '/options', array(
'methods' => 'GET',
'callback' => array ($this, 'add_fcm_option_route')
));
}
/**
* The callback for the `wp/v2/thorfcmapi/options` endpoint
*/
function add_fcm_option_route(WP_REST_Request $request) {
if($request['option']) {
return get_field($request['option'], 'option');
}
return get_fields('option');
}
Когда я делаю эту команду надстройку ред моего URL
?rest_route=/
Я не нахожу мой маршрут в.ч./v2/thorfcmapi в списке маршрутов
Если взять тот же код, сделать отдельный плагин только с этим кодом
add_action('rest_api_init', 'add_thor_FCM_Route_test');
function add_thor_FCM_Route_test() {
register_rest_route('wp/v2/thorfcmapi', '/options', array(
'methods' => GET,
'callback' => 'add_FCM_Option_Route_test'
));
}
/**
* The callback for the `wp/v2/thorfcmapi/options` endpoint
*/
function add_FCM_Option_Route_test(WP_REST_Request $request) {
if($request['option']) {
return get_field($request['option'], 'option');
}
return get_fields('option');
}
только отличается тем, что я не встраивать в классе, и я не использую $ это, она работает зарегистрироваться в качестве маршрута. И я могу сделать вызов API.
Я не хочу плагинов, я хочу, чтобы код работал в моем классе - я взял упрощенный код и добавил его в свой плагин (1-й пример), я добавил его за пределами класса, он все еще не регистрировался маршрут и отсутствие ошибок.
Что я делаю неправильно?
Что я, кажется, не понимаю, чтобы заставить его работать.