WordPress 中的自定义重写规则

发布于 2024-09-07 22:13:41 字数 862 浏览 0 评论 0原文

我在内部 WordPress 重写规则方面遇到了麻烦。 我已阅读此帖子,但仍然无法得到任何结果: WordPress 插件中的 wp_rewrite

我解释一下我的情况:

1)我有一个名为“myplugin_template.php”的 page_template,与名为“mypage”的 WordPress 页面关联。

<?php
get_header();
switch ($_GET['action']) {
  case = "show" {
  echo $_GET['say'];
  }
}
get_footer();
?>

2)我需要为此链接创建重写规则:

http://myblog/index.php?pagename=mypage& ;action=show&say=hello_world

如果我使用这个网址,所有的事情都可以正常工作,但我想达到这个结果:

http://myblog/mypage/say/hello_world/

我真的不想破解我的 .htaccess 文件,但我不知道如何可以使用内部 WordPress 重写器来完成此操作。

I'm in trouble with the internal wordpress rewrite rules.
I've read this thread but I still can't get any results: wp_rewrite in a WordPress Plugin

I explain my situation:

1) I have a page_template called 'myplugin_template.php' associated to a wordpress page called "mypage".

<?php
get_header();
switch ($_GET['action']) {
  case = "show" {
  echo $_GET['say'];
  }
}
get_footer();
?>

2) I need to create a rewrite rule for this link:

http://myblog/index.php?pagename=mypage&action=show&say=hello_world

If I use this url all the things works without problems but I'd like to achieve this result:

http://myblog/mypage/say/hello_world/

I really don't want to hack my .htaccess file but I don't know how I can do this with the internal wordpress rewriter.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

回梦 2024-09-14 22:13:41

您需要添加自己的重写规则和查询变量 - 将其弹出到 functions.php 中;

function my_rewrite_rules($rules)
{
    global $wp_rewrite;

    // the slug of the page to handle these rules
    $my_page = 'mypage';

    // the key is a regular expression
    // the value maps matches into a query string
    $my_rule = array(
        'mypage/(.+)/(.+)/?' => 'index.php?pagename=' . $my_page . '&my_action=$matches[1]&my_show=$matches[2]'
    );

    return array_merge($my_rule, $rules);
}
add_filter('page_rewrite_rules', 'my_rewrite_rules');


function my_query_vars($vars)
{
    // these values should match those in the rewrite rule query string above
    // I recommend using something more unique than 'action' and 'show', as you
    // could collide with other plugins or WordPress core
    $my_vars = array(
        'my_action',
        'my_show'
    );

    return array_merge($my_vars, $vars);
}
add_filter('query_vars', 'my_query_vars');

现在在您的页面模板中,将 $_GET[$var] 替换为 get_query_var($var) ,如下所示;

<?php
get_header();
switch (get_query_var('my_action')) {
    case = "show" {
        echo esc_html(get_query_var('my_say')); // escape!
    }
}
get_footer();
?>

You'll need to add your own rewrite rule and query vars - pop this in functions.php;

function my_rewrite_rules($rules)
{
    global $wp_rewrite;

    // the slug of the page to handle these rules
    $my_page = 'mypage';

    // the key is a regular expression
    // the value maps matches into a query string
    $my_rule = array(
        'mypage/(.+)/(.+)/?' => 'index.php?pagename=' . $my_page . '&my_action=$matches[1]&my_show=$matches[2]'
    );

    return array_merge($my_rule, $rules);
}
add_filter('page_rewrite_rules', 'my_rewrite_rules');


function my_query_vars($vars)
{
    // these values should match those in the rewrite rule query string above
    // I recommend using something more unique than 'action' and 'show', as you
    // could collide with other plugins or WordPress core
    $my_vars = array(
        'my_action',
        'my_show'
    );

    return array_merge($my_vars, $vars);
}
add_filter('query_vars', 'my_query_vars');

Now in your page template, replace $_GET[$var] with get_query_var($var) like so;

<?php
get_header();
switch (get_query_var('my_action')) {
    case = "show" {
        echo esc_html(get_query_var('my_say')); // escape!
    }
}
get_footer();
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文