2

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

3

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
    }
6
  • 1
    why both array_key_exists and isset ? :) Commented Jul 26, 2011 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
    Commented Jul 26, 2011 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
    Commented Jul 26, 2011 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
    Commented Jul 28, 2011 at 0:49
  • You should read this answer on the difference between isset and array_key_exists. You never need to use both.
    – Cobby
    Commented Apr 11, 2012 at 2:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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