admin管理员组

文章数量:1410712

I am trying to learn how WordPress uses the rewrite API to rewrite pretty to ugly URLs . I have some ideas as to how rewrites work in .htaccess file but not how WP handles it . I am reading through the source code and also from a book . So my understanding is that the .htaccess file that comes with WP in the root just checks whether the request is for a file like wp-login.php or not and if not it forwards the request to index.php . The actual rewrite rules are stored in the DB viz. rewrite_rules (in wp_options table) . In the source code is see WP::parse_request call

$rewrite = $wp_rewrite->wp_rewrite_rules();

If I do var_dump($rewrite) , I see an array which I assume are the rewrite rules

Inside WP_Rewite::rewrite_rules() I see

return $this->rules;

So , I’m assuming $wp_rewrite->rules should have all the rules based on my permalink settings .If I var_dump ($wp_rewrite) in functions.php the rules property is always set to be NULL . The var_dump is given below :

WP_Rewrite (object) [Object ID #597][24 properties]
permalink_structure: (string) "/%postname%/"
use_trailing_slashes: (boolean) true 
author_base: (string) "author"
search_base: (string) "search"
comments_base: (string) "comments"
pagination_base: (string) "page"
comments_pagination_base: (string) "comment-page"
feed_base: (string) "feed"
front: (string) "/"
root: (string) ""
index: (string) "index.php"
matches: (string) ""
rules: (null) NULL

The book that I’m following shows the rules property to have the rules

WP_Rewrite Object (
...
[permalink_structure] = > /%year%/%postname%/
[use_trailing_slashes] = > 1
...
[rules] = > Array (
[category/(.+?)/?$] = > index.php?category_name=$matches[1]
[tag/([^/]+)/page/?([0-9]{1,})/?$] = > index.php?tag=$matches[1] & paged=

$matches[2]
[tag/([^/]+)/?$] = > index.php?tag=$matches[1]
[(.+?)/trackback/?$] = > index.php?pagename=$matches[1] & tb=1
...
)
[endpoints] = > Array ()
...
)

My question is why is the $wp_rewrite->rules empty ? Where are the rules actually stored in code?

My second question is I understand that the actual URL rewrite is done using the .htaccess file (like it says in permalinks/)

When you create or update a “pretty” permalink structure, WordPress will generate rewrite rules and attempt to insert them into the proper .htaccess file.

However , the .htaccess file that I see in the root seems to have the same rule all the time . Is WP creating these rules in some other .htaccess files ( in different subfolders perhaps) ?

I am trying to learn how WordPress uses the rewrite API to rewrite pretty to ugly URLs . I have some ideas as to how rewrites work in .htaccess file but not how WP handles it . I am reading through the source code and also from a book . So my understanding is that the .htaccess file that comes with WP in the root just checks whether the request is for a file like wp-login.php or not and if not it forwards the request to index.php . The actual rewrite rules are stored in the DB viz. rewrite_rules (in wp_options table) . In the source code is see WP::parse_request call

$rewrite = $wp_rewrite->wp_rewrite_rules();

If I do var_dump($rewrite) , I see an array which I assume are the rewrite rules

Inside WP_Rewite::rewrite_rules() I see

return $this->rules;

So , I’m assuming $wp_rewrite->rules should have all the rules based on my permalink settings .If I var_dump ($wp_rewrite) in functions.php the rules property is always set to be NULL . The var_dump is given below :

WP_Rewrite (object) [Object ID #597][24 properties]
permalink_structure: (string) "/%postname%/"
use_trailing_slashes: (boolean) true 
author_base: (string) "author"
search_base: (string) "search"
comments_base: (string) "comments"
pagination_base: (string) "page"
comments_pagination_base: (string) "comment-page"
feed_base: (string) "feed"
front: (string) "/"
root: (string) ""
index: (string) "index.php"
matches: (string) ""
rules: (null) NULL

The book that I’m following shows the rules property to have the rules

WP_Rewrite Object (
...
[permalink_structure] = > /%year%/%postname%/
[use_trailing_slashes] = > 1
...
[rules] = > Array (
[category/(.+?)/?$] = > index.php?category_name=$matches[1]
[tag/([^/]+)/page/?([0-9]{1,})/?$] = > index.php?tag=$matches[1] & paged=

$matches[2]
[tag/([^/]+)/?$] = > index.php?tag=$matches[1]
[(.+?)/trackback/?$] = > index.php?pagename=$matches[1] & tb=1
...
)
[endpoints] = > Array ()
...
)

My question is why is the $wp_rewrite->rules empty ? Where are the rules actually stored in code?

My second question is I understand that the actual URL rewrite is done using the .htaccess file (like it says in https://wordpress/support/article/using- permalinks/)

When you create or update a “pretty” permalink structure, WordPress will generate rewrite rules and attempt to insert them into the proper .htaccess file.

However , the .htaccess file that I see in the root seems to have the same rule all the time . Is WP creating these rules in some other .htaccess files ( in different subfolders perhaps) ?

Share Improve this question asked Jan 16, 2020 at 15:57 KnownowKnownow 1216 bronze badges 1
  • 1 How/where are you doing the var_dump ($wp_rewrite) in functions.php? Is it within a hook? – czerspalace Commented Jan 16, 2020 at 18:16
Add a comment  | 

1 Answer 1

Reset to default 3

Your rewrite rules aren't showing up because you're calling it from functions.php before they've been defined on a global level. So, you can either hook into an action or pull the rules directly from the DB.

Doing it through a hook:

function check_my_rewrites() {
  if ( is_admin() ) var_dump($wp_rewrite->wp_rewrite_rules());
}

add_action( 'init','check_my_rewrites' );

Otherwise, you can use the following SQL to pull the values: SELECT option_value FROM wp_options WHERE option_name = 'rewrite_rules';

Then you can copy and paste the value into an online formatter to look at properly.

本文标签: wprewritegtrules is always NULL