Custom routing for plugins

I am making a plugin that needs a page that can be accessed from the outside, pretty much like an API, and have the url like so,

http://xxxxx/custom_method?parameter=xxxxx&something=xxxx

is there a clean way to do this?

Thanks in advance.

1 Answer
1

The WordPress Way of doing it is using query_vars so first you add you vars to the array:

//add to query vars
function add_query_vars($vars) {
    $new_vars = array('custom_method','cm_parameter');
    $vars = $new_vars + $vars;
    return $vars;
}

add_filter('query_vars', 'add_query_vars');

then you can check in your plugin for the vars:

global $wp; 
    if (array_key_exists('custom_method', $wp->query_vars) && isset($wp->query_vars['custom_method'])){
      //do your stuff
    }

Leave a Comment