最简单的方法是将查询变量添加到WordPress识别的查询变量列表中,并在模板重定向挂钩上检查新添加的查询变量,例如:
add_filter( \'query_vars\', \'se67095_add_query_vars\');
/**
* Add the \'my_plugin\' query variable so WordPress
* won\'t remove it.
*/
function se67095_add_query_vars($vars){
$vars[] = "my_plugin";
return $vars;
}
/**
* check for \'my_plugin\' query variable and do what you want if its there
*/
add_action(\'template_redirect\', \'se67905_my_template\');
function se67905_my_template($template) {
global $wp_query;
// If the \'my_plugin\' query var isn\'t appended to the URL,
// don\'t do anything and return default
if(!isset( $wp_query->query[\'my_plugin\'] ))
return $template;
// .. otherwise,
if($wp_query->query[\'my_plugin\'] == \'helloworld\'){
echo "hello world";
exit;
}
return $template;
}