2016-09-18 15 views
1

私はWordPressプラグイン開発の新人です。私は現在、example.com/calc/addにAPIエンドポイントを持つ追加電卓プラグインを開発しようとしています。プラグインが「POST」リクエスト(コンマ区切りリストの数字)を取り込み、数字を追加した後にデータを返すように、WordPressインストールに新しいURLエンドポイントを追加するにはどうすればよいですか?どうもありがとうございます!カスタムAPIエンドポイントを作成するにはどうすればよいですか?

答えて

6

あなたは以下の、エンドポイントを作成するためのparse_requestフックを使用することができますが、私のプラグインの1

// this example creates endpoint like http://emerico.in/api/v2 

add_action('parse_request', 'endpoint', 0); 
add_action('init', 'add_endpoint'); 

/** 
* @param null 
* @return null 
* @description Create a independent endpoint 
*/ 
function endpoint() 
{ 
    global $wp; 

    $endpoint_vars = $wp->query_vars; 

    // if endpoint 
    if ($wp->request == 'api/v2') { 

     // Your own function to process end pint 
     $this->processEndPoint($_REQUEST); 

     // After all redirect to home page 
     wp_redirect(home_url()); 
     exit; 
    } elseif (isset($endpoint_vars['tracking']) && !empty($endpoint_vars['tracking'])) { 
     $request = [ 
      'tracking_id' => $endpoint_vars['tracking'] 
     ]; 

     $this->processEndPoint($request); 
    } elseif (isset($_GET['utm_source']) && !empty($_GET['utm_source'])){ 
     $this->processGoogleTracking($_GET); 
    } 
} 

/** 
* @param null 
* @return null 
* @description Create a permalink endpoint for projects tracking 
*/ 
function add_endpoint() 
{ 

    add_rewrite_endpoint('tracking', EP_PERMALINK | EP_PAGES, true); 

} 
+0

から一例である可能性があり私は何 'add_rewrite_endpoint(「追跡」、EP_PERMALINK | EP_PAGES、真の);'のでしょうか? – EndenDragon

+0

私はこれを使用してクエリのトラッキングvarを追加しています。ただし、https://codex.wordpress.org/Rewrite_API/add_rewrite_endpointから詳細な情報を得ることができます –

関連する問題