Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress Plug-ins: How-to add custom URL Handles

Tags:

wordpress

I'm trying to write a Wordpress Plug-in but can't seem to figure out how you would modify how a URL gets handled, so for example: any requests made for:

<url>/?myplugin=<pageID>

will get handled by a function in my plug-in. I'm sure this is a very simple to do, but I'm pretty new to working with Wordpress and couldn't find it in the documentation.

like image 826
KeyboardInterrupt Avatar asked Nov 07 '09 21:11

KeyboardInterrupt


2 Answers

In order to handle just a specific URL use the code below:

add_action('parse_request', 'my_custom_url_handler');

function my_custom_url_handler() {
   if(isset($_GET['myplugin']) && $_SERVER["REQUEST_URI"] == '/custom_url') {
      echo "<h1>TEST</h1>";
      exit();
   }
}
like image 94
zarazan Avatar answered Oct 28 '22 15:10

zarazan


add_action('parse_request', 'my_custom_url_handler');
function my_custom_url_handler() {
  if( isset($_GET['myplugin']) ) {
    // do something
    exit();
  }
}

That should set you on the right direction. parse_request happens before WordPress runs any of the complicated WordPress queries used to get the posts for the current URL.

like image 29
nickohrn Avatar answered Oct 28 '22 14:10

nickohrn