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

[亡魂溺海] 提交于 2019-12-09 09:23:32

问题


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.


回答1:


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.




回答2:


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();
   }
}


来源:https://stackoverflow.com/questions/1694563/wordpress-plug-ins-how-to-add-custom-url-handles

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!