I am writing a plugin that needs to trigger an action based on URL’s. The url scheme is the following:
mywordpresssite.com/action/12345
where 12345
is a unique code used by the function triggered. My question is how can I trigger a function in the plugin based on such a link?
EDIT
Thanks to the answers below I wrote the 3 following functions but I don’t have what I want yet. add_
function add_endpoint(){
error_log('add_endpoint');
add_rewrite_endpoint( 'action', EP_ROOT );
}
add_action('init', 'add_endpoint', 0);
function add_query_vars($vars){
error_log("query");
$vars[] = 'action';
return $vars;
}
add_filter('query_vars', add_query_vars, 0);
function sniff_requests(){
global $wp;
error_log("request sniffed:".$wp->query_vars['action']);
}
add_filter('parse_request', sniff_requests, 0);
And the log says that all functions are triggered but it fails to display $wp->query_vars['action']
. My guess is that the rewrite rule is not recognized by the system:
[26-Aug-2013 22:22:35 UTC] add_endpoint
[26-Aug-2013 22:22:35 UTC] query
[26-Aug-2013 22:22:35 UTC] request sniffed:
As @toscho says you need an endpoint.
Note code is untested.