I’m trying to add a rewrite rule to WordPress. I want to rewrite: “http://www.domain.com/plm_check/var_a/var_b/var_c/” to actually use a file from “http://www.domain.com/wp-content/plugins/plm/webservices/plm_check.php?var_a=…&var_b=…&var_c=…”
I’ve registered it like this:
/* Register rewrites for web service calls */
add_action( 'generate_rewrite_rules' , 'register_plm_rewrites');
function register_plm_rewrites() { //
global $wp_rewrite;
$wp_rewrite->add_external_rule( '^plm_check/([a-zA-z0-9_]+)/([a-zA-z0-9-]+)/([0-9]+)/?$' , PLM_PLUGIN_URL . 'webservices/plm_check.php' );
// Flush the rules
$wp_rewrite->flush_rules();
}
Instead of add_external_rule, I’ve tried add_rewrite_rule like this:
$wp_rewrite->add_rewrite_rule( '^plm_check/([a-zA-z0-9_]+)/([a-zA-z0-9-]+)/([0-9]+)/?$' , PLM_PLUGIN_URL . 'webservices/plm_check.php' , 'top' );
I’ve also tried it with and without $wp_rewrite.
I think the problem is that WordPress won’t allow me to directly access that file as a URL. For instance, if I just put in “http://www.domain.com/wp-content/plugins/plm/webservices/plm_check.php”, it takes me to the 404 page, even though the file actually exists.
Am I using these functions wrong? Hooking to the wrong action? Trying to do something that WP isn’t going to allow?
Thanks!
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:
parse_request
action 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.
In your plugin file
plm_check.php
, you can access your query vars like:In your case
add_rewrite_rul()
will calladd_external_rule()
which collects rules to be written into you .htaccess file. If your .htaccess is write protected, as it should be, it will not work. I also not sure that your rewrite rules will work at all, you should probably try them first by manually editing your .htaccess.In addition, what @milo commented.