Here’s a quick (hopefully error-free!) example of adding an internal rewrite and loading a plugin file to process those requests. This will give you access to the WordPress environment so you can use the database, etc..
The general steps are:
- add your rewrite and point it to index.php with your custom query vars appended
- register your custom query vars so WordPress knows what they are
- hook the
parse_requestaction and check if one of the query vars is set. if it is, load the plugin file and exit before WordPress does the default query and loads the template
.
<?php
/*
Plugin Name: PLG
Plugin URI:
Description:
Author:
Version:
Author URI:
*/
class PLG {
static $plugin_path;
public function __construct(){
$this->plugin_path = plugin_dir_path(__FILE__);
register_activation_hook( __FILE__, array( $this, 'flush' ) );
add_action( 'init', array( $this, 'init') );
add_filter( 'query_vars', array( $this, 'query_vars') );
add_action( 'parse_request', array( $this, 'parse_request') );
}
public function flush(){
$this->init();
flush_rewrite_rules();
}
public function init(){
add_rewrite_rule(
'plm_check/([a-zA-z0-9_]+)/([a-zA-z0-9-]+)/([0-9]+)/?$',
'index.php?plmvar1=$matches[1]&plmvar2=$matches[2]&plmvar3=$matches[3]',
'top'
);
}
public function query_vars( $query_vars ){
$query_vars[] = 'plmvar1';
$query_vars[] = 'plmvar2';
$query_vars[] = 'plmvar3';
return $query_vars;
}
public function parse_request( &$wp ){
if ( array_key_exists( 'plmvar1', $wp->query_vars ) ){
include $this->plugin_path . 'webservices/plm_check.php';
exit();
}
return;
}
}
$wpa8185_plg = new PLG();
In your plugin file plm_check.php, you can access your query vars like:
<?php
global $wp;
echo $wp->query_vars['plmvar1'];