I would like my plugin to route /tradeIns for any domain it's installed on to the template file inside the plugin filder's templates/checkout.php. In my local dev site the desired url route would be http://localhost/wptest2/tradeIns

Attempting to use add_rewrite_rule() I have written this:

function addCheckoutRedirect() {
    , 'localhost/wptest2/wp-content/plugins/pluginNake/templates/checkout.php'
add_action('init', 'addCheckoutRedirect', 10, 0);

This is a copy/paste modification of the "non index.php" example at: https://codex.wordpress.org/Rewrite_API/add_rewrite_rule

I'm getting an Oops, page not found error on my test site. It could be the regex match.

I suspect it may be the hardcoded path string in the 2nd argument above. I switched /var/html/www/wptest2/ to localhost/wptest2/ which is the path-to-url difference for other pages like the home page, but it may not be working here.

Can someone advise me on the correct way to write the rewrite url here (statically or dynamically)? Or if the regex is the actual issue I'd appreciate any feedback on that too, thank you.


The path should just start with wp-content because the generated rewrite rule (which would be added to your .htaccess) is relative to your WordPress installation directory:

function addCheckoutRedirect() {
        'tradeIns', // I intentionally removed the caret (^)

And here's how your .htaccess file may look like after the addition of the above "non index.php" rule: (in this example, WordPress is installed in the root directory; hence the RewriteBase is /)

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
# WordPress automatically added the caret (^), and the slash (/) before wp-content
RewriteRule ^tradeIns /wp-content/plugins/pluginNake/templates/checkout.php [QSA,L]
# END WordPress

Don't forget to flush the rewrite rules — just visit the permalink settings page.

Alternate Option

Create a standard Page (post of the page type), give it the slug tradeIns and use the page_template hook to load the checkout.php page when the tradeIns page is being requested.

add_filter( 'page_template', function ( $template ) {
    return is_page( 'tradeIns' )
        ? '/full/path/to/plugins/pluginNake/templates/checkout.php'
        : $template;
} );

Using this option gives you the advantage that you don't need any custom rewrite rules or the need to set your plugin's checkout.php as a custom Page template.

But the final decision is yours; just use whichever option is best for your specific needs.

Another Option: Completely Dynamic URL

Which means you don't need any custom rewrite rules and no need to create any Pages.

And this example uses the parse_request hook, but you may also use the wp hook or a similar hook. However, when using the wp hook (or any hooks where WordPress already sent the headers), you would want to call status_header( 200 ); to announce a 200 HTTP status header. And that is to prevent a "not found"/404 error since the path (tradeIns in the example below) doesn't actually exist (not a WordPress Page, etc.).

add_action( 'parse_request', function ( $wp ) {
    if ( 'tradeIns' === $wp->request ) { // the request is example.com/tradeIns
        //status_header( 200 );
        require_once '/full/path/to/plugins/pluginNake/templates/checkout.php';
} );
  • Thank you, I am investigating a plugin-only way to implement your alternate suggestion. insert_post() may be able to do it. Do you know how to set the post content to a require_once on a .php template file? That is my current snag. – Sean Dezoysa Oct 6 at 17:10
  • I don't really understand that.. perhaps you can post another question and clarify what you mean? But yes, plugins can use wp_insert_post() to programmatically create new Pages/posts. – Sally CJ Oct 7 at 9:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.