在类库中注册休息路由不起作用

问题描述:

我在构建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'); 
} 

当我做这个命令添加编辑我的网址

?rest_route=/ 

我不觉得在路线

列表我的路线WP/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'); 

}

唯一不同的是,我没有嵌入到一个类中,我不使用$ this,它的工作原理是注册为路由。我可以做一个API调用。

我不想插件,我想在我的课程中使用代码 - 我将简化代码添加到我的插件中(第一个示例),我在课程之外首先添加了它,但它仍未注册路线和没有错误。

我在做什么错?

这是什么,我似乎不明白,使其工作。

我发现问题 - 它与使用is_admin()有关 - 如果is_admin为true,我无法注册休息路由。案件结案。