Created
January 29, 2019 07:37
-
-
Save orangerdev/29fd26dc28dc6df08115ccd874d27a59 to your computer and use it in GitHub Desktop.
basic wordpress custom endpoint
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* | |
* Change 'custom-request' to anything you want | |
* | |
* Register custom rules | |
* Hooked via filter generate_rewrite_rules, priority 999 | |
* @param Object $wp_rewrite | |
*/ | |
function set_custom_rules($wp_rewrite) | |
{ | |
$wp_rewrite->rules = array_merge([ | |
'custom-request/([^/]+)/?$' => 'index.php?custom-request=$matches[1]' | |
],$wp_rewrite->rules); | |
} | |
/** | |
* Add custom query vars | |
* Hooked via filter query_vars, priority 999 | |
* @param array $vars | |
* @return array | |
*/ | |
function add_query_vars($vars) | |
{ | |
$vars[] = 'custom-request'; | |
return $vars; | |
} | |
/** | |
* Check endpoint request | |
* Hooked via action template_redirect, priority 999 | |
* @return void | |
*/ | |
function check_request() | |
{ | |
global $wp_query; | |
if(get_query_var('valor-data')) : | |
header("HTTP/1.1 200 OK"); | |
if('cart' === get_query_var('valor-data')) : | |
$this->do_cart(); | |
elseif('submit' === get_query_var('valor-data')) : | |
$this->do_order(); | |
endif; | |
endif; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment