Create custom rewrite rule for my WordPress plugin
Posted
by kitsched
on Stack Overflow
See other posts from Stack Overflow
or by kitsched
Published on 2009-10-13T09:33:43Z
Indexed on
2010/03/22
9:51 UTC
Read the original article
Hit count: 656
I'm writing a plug-in for WordPress which in fact will be a separate ordering module (it will be placed in an IFRAME on the site I'm developing as well as others) but with its admin tied into WordPress. I wrote the administration part without too much hassle, however I'm having trouble with the front-end.
First of all I'd like my script to be accessible via www.mysite.com/order/ and, as per the WordPress codex, I found I need to place the following code into my main plugin file:
add_action('init', 'ta_flush_rewrite_rules');
function ta_flush_rewrite_rules()
{
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
add_action('generate_rewrite_rules', 'ta_add_rewrite_rules');
function ta_add_rewrite_rules( $wp_rewrite ) {
$new_rules = array("order/(.+)" => "/wp-content/plugins/my-plugin/order.php");
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}
But it doesn't work and I don't really want to get dirty with .htaccess hacking.
Furthermore even if this would work, the order.php file is a separate file from my plugin. This means that I'll have to include some WordPress files in order to have access to the database and other helper classes and functions. That brings us to question number 2: is there a way for the URL to call a function of my plugin to render the order page?
© Stack Overflow or respective owner