Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

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
    }
share|improve this answer
1  
why both array_key_exists and isset ? :) –  onetrickpony Jul 26 '11 at 8:25
    
first we make sure its a valid acceptable query_var, and then if it has a value to avoid warnings and notices. –  Bainternet Jul 26 '11 at 8:27
1  
Both effectively do the same thing though don't they? .. neither checks if the value is an empty string, they just check the key is set... am i missing something? –  t31os Jul 26 '11 at 11:11
    
Thanks, this did it. At first I did not understand the mechanism of query_var and was confused but after looking at the Codex codex.wordpress.org/Custom_Queries and print_ring the $wp->query_var, I now have a better understanding of how it works. –  Saifis Jul 28 '11 at 0:49
    
You should read this answer on the difference between isset and array_key_exists. You never need to use both. –  Cobby Apr 11 '12 at 2:32
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.