add_rewrite_rule: Issue converting path to url

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() {
    add_rewrite_rule(
        'tradeIns', // I intentionally removed the caret (^)
        'wp-content/plugins/pluginNake/templates/checkout.php',
        'top'
    );
}

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';
        exit;
    }
} );

Leave a Comment