pages - add_rewrite_rule: Issue converting path to url

I would like my plugin to route tradeIns for any domain it's installed on to the template file inside the plugin f

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() {
  add_rewrite_rule(
    '^tradeIns'
    , 'localhost/wptest2/wp-content/plugins/pluginNake/templates/checkout.php'
    ,'top'
  );
}
add_action('init', 'addCheckoutRedirect', 10, 0);

This is a copy/paste modification of the "non index.php" example at:

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.

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() {
  add_rewrite_rule(
    '^tradeIns'
    , 'localhost/wptest2/wp-content/plugins/pluginNake/templates/checkout.php'
    ,'top'
  );
}
add_action('init', 'addCheckoutRedirect', 10, 0);

This is a copy/paste modification of the "non index.php" example at: https://codex.wordpress/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.

Share Improve this question edited Oct 6, 2019 at 7:02 Sean D asked Oct 6, 2019 at 6:57 Sean DSean D 3878 silver badges21 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

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/tradeIns
        //status_header( 200 );
        require_once '/full/path/to/plugins/pluginNake/templates/checkout.php';
        exit;
    }
} );

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745103403a4611412.html

相关推荐

  • pages - add_rewrite_rule: Issue converting path to url

    I would like my plugin to route tradeIns for any domain it's installed on to the template file inside the plugin f

    12小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信