WIKI使用導(dǎo)航
站長(zhǎng)百科導(dǎo)航
站長(zhǎng)專題
- 網(wǎng)站推廣
- 網(wǎng)站程序
- 網(wǎng)站賺錢
- 虛擬主機(jī)
- cPanel
- 網(wǎng)址導(dǎo)航專題
- 云計(jì)算
- 微博營(yíng)銷
- 虛擬主機(jī)管理系統(tǒng)
- 開(kāi)放平臺(tái)
- WIKI程序與應(yīng)用
- 美國(guó)十大主機(jī)
WordPress:Query Overview
This article is an overview for developers of the process WordPress uses to build your blog pages, and how plugins can modify that process. It is aimed at developers of plugins that will do advanced queries and permalinks, and also at developers who want to understand WordPress better, in order to add new features to the core of WordPress or fix WordPress bugs.
這篇文章是寫給開(kāi)發(fā)人員的概述,關(guān)于創(chuàng)建博客網(wǎng)頁(yè)的過(guò)程,以及插件怎樣更改這個(gè)過(guò)程。這篇文章是寫給插件開(kāi)發(fā)人員的,他們做高級(jí)查詢和permalinks,同時(shí)也是寫給想要更深入地了解WordPress的開(kāi)發(fā)人員的,想要向WordPress核心添加新的功能或者解決WordPress程序錯(cuò)誤的開(kāi)發(fā)人員。
For more details, you'll need to read the WordPress core PHP files and functions mentioned.
更多的信息,你需要閱讀WordPress核心PHP文件和提及的函數(shù)。
So, here are the steps WordPress uses to decide what posts or pages to display on a page, and display them:
WordPress執(zhí)行下面的步驟,決定在一個(gè)網(wǎng)頁(yè)上顯示那篇文章或者那些網(wǎng)頁(yè),并且顯示這些文章或者網(wǎng)頁(yè)。
- When a visitor first clicks on or types a URL for a page that is part of your blog, WordPress starts by running a few core files (wp-config.php, wp-settings.php, etc.) If you are interested in the specifics of the file loading order, start at index.php and follow the chain of files as each PHP file includes/requires additional PHP files.
- 當(dāng)訪客首次點(diǎn)擊你的博客網(wǎng)頁(yè)或者輸入你的博客中一個(gè)網(wǎng)頁(yè)的URL時(shí),WordPress就開(kāi)始運(yùn)行一些核心文件(wp-config.php, wp-settings.php, 等等。)如果你對(duì)文件載入順序的詳細(xì)信息,感興趣,你可以從index.php開(kāi)始,緊跟著一系列文件,因?yàn)槊總€(gè)PHP文件包含/需要額外的PHP文件。
- WordPress loads and initializes any plugins you have activated (calls the plugin init actions).
- 任何你激活的插件,WordPress都會(huì)載入并且對(duì)其初始化(稱作插件 init活動(dòng))。
- WordPress loads the "text domain" for internationalization, and the functions.php file from the currently active theme.
- 為了國(guó)際化的需求,WordPress載入了"text domain",同時(shí)從當(dāng)前運(yùn)行的主題中,載入functions.php文件。
- WordPress runs the wp() function (in wp-includes/functions.php), which calls $wp->main() ($wp is an object of class WP, which is defined in wp-includes/classes.php). This tells WordPress to:
- WordPress運(yùn)行wp()函數(shù)(在wp-includes/functions.php中),調(diào)用$wp->main() ($wp is an object of class WP, 定義在wp-includes/classes.php中)。這命令WordPress:
- Parse the URL into a query specification using WP->parse_request() -- more on that below.
- 使用WP->parse_request()將URL解析為查詢specification—下面有更多的內(nèi)容。
- Set all the is_ variables that are used by WordPress:Conditional Tags using $wp_query->parse_query() ($wp_query is an object of class WP_Query, which is defined in wp-includes/query.php). Note that in spite of this function's name, in this case WP_Query->parse_query doesn't actually do any parsing for us, since that is done before-hand by WP->parse_request().
- 使用$wp_query->parse_query()($wp_query is an object of class WP_Query, 定義在wp-includes/query.php中),設(shè)置條件式標(biāo)簽使用的所有參數(shù)。注意盡管函數(shù)的名稱是那樣的,在這個(gè)例子中,WP_Query->parse_query不為我們做任何解析,因?yàn)榻馕鍪?tt>WP->parse_request()預(yù)先執(zhí)行的。
## Convert the query specification into a MySQL database query, and run the database query to get the list of posts, in function WP_Query->get_posts(). Save the posts in the $wp_query object to be used in the WordPress Loop.
- 將查詢specification轉(zhuǎn)換為MySQL數(shù)據(jù)庫(kù)查詢,運(yùn)行數(shù)據(jù)庫(kù)查詢得到函數(shù)WP_Query->get_posts()中文章的列表。將文章保存到用在WordPress Loop中的$wp_query object。
- Handle 404 errors.
- 處理404錯(cuò)誤。
- Send the blog's HTTP headers.
- 發(fā)送博客的HTTP標(biāo)頭。
- Set up some variables for the WordPress Loop.
- 為WordPress Loop設(shè)置一些變數(shù)。
- WordPress loads your template, figures out which template file to use according to the WordPress:Template Hierarchy, and runs that file (basically doing whatever your template file says to do). Or, WordPress could run one of the feed files (such as wp-rss2.php) instead.
- WordPress載入你的模板,根據(jù)模板層級(jí)決定使用哪個(gè)模板文件,并且運(yùn)行那個(gè)文件(基本上是執(zhí)行模板文件的命令)。或者WordPress可以運(yùn)行其中的一個(gè)feed文件(例如wp-rss2.php)。
- Generally, the template or feed file runs the WordPress Loop to print blog posts or a static page.
- 一般來(lái)說(shuō),模板或者feed文件運(yùn)行WordPress Loop打印博客文章或者靜態(tài)網(wǎng)頁(yè)。
- The template or feed file will also likely print out permalinks to some archives, categories, or posts using built-in WordPress functions.
- 模板或者feed文件也有可能使用內(nèi)置WordPress函數(shù),為一些歸檔,類別,后者文章打印permalinks。
More on WP->parse_request()[ ]
更多關(guān)于 WP->parse_request()的內(nèi)容[ ]
As mentioned above, WP->parse_request() (part of class WP in wp-includes/classes.php) parses a URL into a query specification. Here is a summary of the steps it uses to do this:
如上所述,WP->parse_request() (class WP in wp-includes/classes.php的一部分)將URL解析為查詢specification。下面是這個(gè)步驟的總結(jié):
- Strips the GET variable section out of the URL (i.e. anything after a "?" in the URL). Also strips out the blog's home URL.
- 去除URL中的得到變數(shù)部分(例如,URL中"?"后的任何內(nèi)容)。同時(shí)去除博客主頁(yè)URL。
- Obtains the rewrite rules that are currently in effect, by calling $wp_rewrite->wp_rewrite_rules() ($wp_rewrite is an object of class WP_Rewrite, which is defined in wp-includes/rewrite.php). The rewrite rules are basically a set of pattern matching rules for WordPress permalinks, with a specification of what to do if the pattern matches. For instance, by default there is a rule that would match a stripped permalink like category/abc, and its specification says that it means the "abc" category was requested. There is also a rewrite rule for the home page (nothing after the blog URL).
- 通過(guò)調(diào)用$wp_rewrite->wp_rewrite_rules() ,得到當(dāng)前有效的rewrite rules ($wp_rewrite是an object of class WP_Rewrite, 定義在wp-includes/rewrite.php中)。重寫規(guī)則基本上是一組WordPress permalinks的模式匹配規(guī)則,規(guī)定了模式匹配時(shí),應(yīng)該做什么。例如,默認(rèn)情況下,有一個(gè)規(guī)則會(huì)匹配stripped permalink像 category/abc, 而且規(guī)定的意思是需要"abc" 類別。主頁(yè)也有一個(gè)重寫規(guī)則(博客URL后什么內(nèi)容也沒(méi)有)。
- Goes through the rewrite rules in order, until it finds a match between a rewrite rule and the permalink. If nothing is found, it's a 404 error. If a match is found, WordPress extracts the information according to the rule specification.
- 按順序查看重寫規(guī)則,知道找到重寫規(guī)則和permalink之間的一個(gè)匹配。如果什么匹配也沒(méi)有發(fā)現(xiàn),返回404錯(cuò)誤。如果找到了匹配,WordPress根據(jù)規(guī)則,提取信息。
- Obtains the list of query variables that is currently in effect. For each query variable, WordPress checks to see if it has been set by permalink parsing, POST submission, or GET submission, and if so, WordPress saves the variable's value into the query specification array ($wp->query_vars, part of class WP in wp-includes/classes.php).
- 得到當(dāng)前有效的一列查詢變數(shù)。對(duì)于每個(gè)查詢變數(shù),WordPress檢查看這個(gè)變數(shù)是否由permalink 解析,文章遞交,或者得到遞交設(shè)置了,如果已經(jīng)設(shè)置了,WordPress將變數(shù)的值保存到query specification數(shù)組($wp->query_vars, class WP in wp-includes/classes.php的一部分)。
What Plugins can Modify[ ]
插件可以更改什么[ ]
Here is an overview of the things a plugin can do to modify the default query and permalink behavior described above. Many of these modifications are described (with examples) in the article WordPress:Custom Queries.
下面是一個(gè)概述,關(guān)于插件可以怎樣做,更改默認(rèn)查詢和上述的permalink。許多這樣的更改在自定義查詢中有描述(帶有例子)。
- Add, modify, or remove rewrite rules, to affect how permalinks are parsed. This is generally not done with filters and actions, but instead by calling functions in wp-includes/rewrite.php, such as add_rewrite_rule, add_rewrite_endpoint, etc. This can be a bit tricky, because WP_Rewrite->wp_rewrite_rules() usually just gets the previously-saved set of rewrite rules (they are saved in the WordPress database as option "rewrite_rules"). So if you want to modify rewrite rules, you will need to call $wp_rewrite->flush_rules() to force them to recalculate. You'll need to do this in your plugin's init action, so that it happens early enough in the process.
- 添加,更改,或者移除重寫規(guī)則,會(huì)影響permalinks的解析方式。這些通過(guò)不是過(guò)濾器或者actions完成的,而是通過(guò)調(diào)用wp-includes/rewrite.php中的函數(shù),例如add_rewrite_rule, add_rewrite_endpoint,等等。這樣做有些微妙,因?yàn)?tt>WP_Rewrite->wp_rewrite_rules()通常只得到先前保存的改寫規(guī)則(這些規(guī)則保存在 WordPress 數(shù)據(jù)庫(kù)作為選項(xiàng)"rewrite_rules")。因此,如果你想要更改改寫規(guī)則,你需要在你的插件的init操作中更改,因此更改過(guò)程很早就發(fā)生了。
- Add or remove query variables, to affect which variables are saved in the query specification from POST, GET, and permalink requests (query_vars filter).
- 添加或者移除查詢變數(shù),影響哪個(gè)變數(shù)保存在來(lái)自文章,GET和permalink請(qǐng)求(query_vars filter)的查詢specification。
- Modify the query specification, after variable values are saved (request filter or parse_request action; if you want to use conditional tag tests, use the parse_query or pre_get_posts action, as these run after the is_ variables are set).
- 變數(shù)值保存后,更改query specification(request 過(guò)濾器或者parse_request action; 如果你想使用條件式標(biāo)簽測(cè)試,請(qǐng)使用 parse_query 或者 pre_get_posts action, 因?yàn)?tt>is_ 變數(shù)設(shè)置好之后,這些才會(huì)運(yùn)行)。
- Modify the MySQL database query, after it is created from the query specification (posts_where, posts_join, posts_groupby, posts_orderby, posts_distinct, posts_fields, post_limits, posts_where_paged, posts_join_paged, and posts_request filters).
- MySQL數(shù)據(jù)庫(kù)query從query specification中創(chuàng)建好之后,更改MySQL數(shù)據(jù)庫(kù)query(posts_where, posts_join, posts_groupby, posts_orderby, posts_distinct, posts_fields, post_limits, posts_where_paged, posts_join_paged, 和posts_request 過(guò)濾器)。
- Modify the results of the database query (the_posts filter).
- 更改數(shù)據(jù)庫(kù)query的結(jié)果(the_posts 過(guò)濾器)。
- Override the default template file choice (template_redirect action).
- 廢除默認(rèn)模板文件選擇(template_redirect action)。