简体   繁体   English

Wordpress - wp_rewrite规则

[英]Wordpress - wp_rewrite rules

I'm attempting to write a plugin for WordPress but I'm having some issues with regards to the wp_rewrite functionality. 我正在尝试为WordPress编写一个插件,但是我遇到了一些关于wp_rewrite功能的问题。

I want to make one page appear as its many by passing variables via the URL (such as: www.mysite.com/WordPress?variable=helloall) 我希望通过URL传递变量使一个页面显示为多个页面(例如:www.mysite.com/WordPress?variable = helloall)

However I want to keep the permalink structure intact, so i want the URL to appear as such: 但是我想保持永久链接结构完整,所以我希望URL显示如下:

www.mysite.com/WordPress/helloall

I then want to be able to take the slug and use that the search my database. 然后,我希望能够使用slug并使用搜索我的数据库。 (like you would using $_GET if I was using the general method i mentioned first) (就像我使用$ _GET一样,如果我使用的是我先提到的一般方法)


I have found a few tutorials online and as of yet able to get this working. 我在网上找到了一些教程,但仍然可以使这个工作。 I believe my problem is due to a lack of understand with HOW you write the rules correctly. 我相信我的问题是由于您如何正确编写规则而缺乏理解。

I have used this tutorial: 我使用过这个教程:

http://www.prodeveloper.org/create-your-own-rewrite-rules-in-wordpress.html http://www.prodeveloper.org/create-your-own-rewrite-rules-in-wordpress.html

and I have attempted to use the same code for the most part. 我试图在大多数情况下使用相同的代码。 I am able to set the rules, but they just dont seem to want to work for me 我能够制定规则,但他们似乎不想为我工作

can anyone tell me the correct format to beable to do this? 谁能告诉我正确的格式才能做到这一点?

Edit 编辑

this is my current function 这是我目前的职能

function add_rewrite_rules( $wp_rewrite ) 
{
    $new_rules = array
    (
        '(.?.+?)/(.+?)/page/?([0-9]{1,})/?$' => 'index.php?pagename='.
        $wp_rewrite->preg_index(1).'&varname='.
        $wp_rewrite->preg_index(2).'&page='.
        $wp_rewrite->preg_index(3),

        '(.?.+?)/(.*?)/?$' => 'index.php?pagename='.
        $wp_rewrite->preg_index(1).'&varname='.
        $wp_rewrite->preg_index(2)
    );
    // Always add your rules to the top, to make sure your rules have priority
    $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}

add_action('generate_rewrite_rules', 'add_rewrite_rules'); add_action('generate_rewrite_rules','add_rewrite_rules');

Solution

I have figured it out, I was going to post this as an answer but it seems I am unable to answer my own questions at this moment in time, so instead I'm editing the original post: 我已经弄明白了,我打算将此作为答案发布,但似乎我现在无法回答我自己的问题,所以我正在编辑原帖:

First off, the code I post above is correct, however the reason it was not working is because I was not flushing the rules, I do this with the following code: 首先,我上面发布的代码是正确的,但是它不起作用的原因是因为我没有刷新规则,我使用以下代码执行此操作:

function ebi_flush_rewrite_rules()
{
global $wp_rewrite;

$wp_rewrite->flush_rules();
}

add_action( 'init', 'flush_rewrite_rules');

My new problem, was that my code worked a little to well, redirecting ALL pages instead of just the one I wanted, this meant that no child pages would display which is a bit of an issue, I have however solved the problem with one small edit: 我的新问题是,我的代码工作得很好,重定向所有页面而不是我想要的页面,这意味着没有显示哪个子页面有点问题,但是我已经解决了一个小问题编辑:

function add_rewrite_rules( $wp_rewrite ) 
{
  $new_rules = array
  (
    '(testpage)/(.+?)/page/?([0-9]{1,})/?$' => 'index.php?pagename='.
    $wp_rewrite->preg_index(1).'&varname='.
    $wp_rewrite->preg_index(2).'&page='.
    $wp_rewrite->preg_index(3),

    '(testpage)/(.*?)/?$' => 'index.php?pagename='.
    $wp_rewrite->preg_index(1).'&varname='.
    $wp_rewrite->preg_index(2)
  );
  // Always add your rules to the top, to make sure your rules have priority
  $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}

So my final code regarding the wp_rewrite functionality is as follows: 所以关于wp_rewrite功能的最终代码如下:

function add_rewrite_rules( $wp_rewrite ) 
{
$new_rules = array
(
    '(testpage)/(.+?)/page/?([0-9]{1,})/?$' => 'index.php?pagename='.
    $wp_rewrite->preg_index(1).'&varname='.
    $wp_rewrite->preg_index(2).'&page='.
    $wp_rewrite->preg_index(3),

    '(testpage)/(.*?)/?$' => 'index.php?pagename='.
    $wp_rewrite->preg_index(1).'&varname='.
    $wp_rewrite->preg_index(2)
);
// Always add your rules to the top, to make sure your rules have priority
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}

function query_vars($public_query_vars)
{
$public_query_vars[] = "varname";
return $public_query_vars;
}

function ebi_flush_rewrite_rules()
{
global $wp_rewrite;
$wp_rewrite->flush_rules();
}

add_action( 'init', 'flush_rewrite_rules');
add_action('generate_rewrite_rules', 'add_rewrite_rules');
add_filter('query_vars', 'query_vars');

I hope this saves someone else some time in the future. 我希望将来能节省一些时间。

I figured out how to get it working on ea specific page name, this is for anyone who is having issues in the future: 我想出了如何让它在ea特定的页面名称上运行,这适用于将来遇到问题的任何人:

function add_rewrite_rules( $wp_rewrite ) 
{
    $new_rules = array
    (
        '(testpage)/(.+?)/page/?([0-9]{1,})/?$' => 'index.php?pagename='.
        $wp_rewrite->preg_index(1).'&varname='.
        $wp_rewrite->preg_index(2).'&page='.
        $wp_rewrite->preg_index(3),

        '(testpage)/(.*?)/?$' => 'index.php?pagename='.
        $wp_rewrite->preg_index(1).'&varname='.
        $wp_rewrite->preg_index(2)
    );
    // Always add your rules to the top, to make sure your rules have priority
    $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}

function query_vars($public_query_vars)
{
    $public_query_vars[] = "varname";

    return $public_query_vars;
}

function ebi_flush_rewrite_rules()
{
    global $wp_rewrite;

    $wp_rewrite->flush_rules();
}

add_action( 'init', 'flush_rewrite_rules');
add_action('generate_rewrite_rules', 'add_rewrite_rules');
add_filter('query_vars', 'query_vars');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM