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

后端 未结 2 1381
春和景丽
春和景丽 2021-02-14 10:14

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:

相关标签:
2条回答
  • 2021-02-14 10:48

    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();
       }
    }
    
    0 讨论(0)
  • 2021-02-14 11:11
    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.

    0 讨论(0)
提交回复
热议问题