Skip to content

Instantly share code, notes, and snippets.

@orangerdev
Created January 29, 2019 07:37
Show Gist options
  • Save orangerdev/29fd26dc28dc6df08115ccd874d27a59 to your computer and use it in GitHub Desktop.
Save orangerdev/29fd26dc28dc6df08115ccd874d27a59 to your computer and use it in GitHub Desktop.
basic wordpress custom endpoint
<?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