添加_重写_规则:将路径转换为URL时出现问题

时间:2019-10-06 作者:Sean D

我想让我的插件将它安装到的任何域的路由/交易到插件filder中的模板文件中templates/checkout.php. 在我的本地开发人员站点中,所需的url路由为http://localhost/wptest2/tradeIns

正在尝试使用add_rewrite_rule() 我写了以下内容:

function addCheckoutRedirect() {
  add_rewrite_rule(
    \'^tradeIns\'
    , \'localhost/wptest2/wp-content/plugins/pluginNake/templates/checkout.php\'
    ,\'top\'
  );
}
add_action(\'init\', \'addCheckoutRedirect\', 10, 0);
这是对“non-index.php”示例的复制/粘贴修改,位于:https://codex.wordpress.org/Rewrite_API/add_rewrite_rule

我得到一个Oops, page not found 我的测试站点出错。可能是正则表达式匹配。

我怀疑这可能是上面第二个参数中的硬编码路径字符串。我切换了/var/html/www/wptest2/localhost/wptest2/ 这是其他页面(如主页)的url差异路径,但在这里可能不起作用。

有人能告诉我在这里写重写url的正确方法(静态或动态)吗?或者如果regex是真正的问题,我也希望得到任何反馈,谢谢。

1 个回复
最合适的回答,由SO网友:Sally CJ 整理而成

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

相关推荐

List pages by custom field?

出于SEO目的,我使用的URL如下:地点com/父/子/目前我有如下页面:地点com/父级/地点com/第1页/地点com/第2页/地点com/第3页/我想列出some 网站上的第1页、第2页等页面。com/parent/I认为最好的方法是在这些字段中添加一个自定义字段。那么,有没有更好的方法来做到这一点?如果没有,如何根据自定义字段列出页面?