私はWPプラグインのビルドに関する2つの問題を実行しました。問題は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=/
にエド私は同じコードを取る場合は、ちょうどこのコード
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');
で別のプラグインを作るルート
のリストにWP/V2/thorfcmapi私のルートを見つけることができません。
唯一の違いは、クラス内に埋め込まれていないことです。私は$ thisを使用しません。ルートとして登録されます。私はAPI呼び出しを行うことができます。
私はプラグインをしたくありません。コードを自分のクラスに入れたい - 簡単なコードを取り込み、プラグインで追加しました(最初の例)。クラスの外に最初に追加しました。ルートとエラーなし。
私は間違っていますか?
私はそれを動作させるために理解していないようです。