wordpress进阶教程(16):添加一个重写规则,构建新页面初试
前面一篇教程我们介绍了wordpress url重写是怎么工作的,这一篇教程我们来个小例子,加深对wordpress url重写的认识。
今天的例子要做到的(这是wordpress自定义会员系统的雏形哦):
以默认的twenty ten 主题为例,我们现在默认主题中新建一个user文件夹,文件夹中添加一个php文件,里面的代码为:
- <?php
- /*********用户欢迎页面************/
- get_header(); //载入头部文件
- if( !is_user_logged_in()) { //判断用户是否登录
- echo '<div style="text-align:center; margin:40px;">访客您好,请先登录。</div>';
- }else{
- global $current_user;
- get_currentuserinfo();
- $uname = $current_user->user_nicename;
- echo '<div style="text-align:center; margin:40px;"><span style="color:red;">';
- echo $uname; //输出用户名
- echo '</span>您好,欢迎登陆...</div>';
- }
- get_footer(); //载入底部文件
- ?>
这个文件通过判断用户分别输出一句欢迎语句。效果图(点击图片查看大图)
实际操作:
一、添加翻译规则。首先我们前面介绍了url的翻译规则,我们要往翻译规则中添加一条自己的翻译规则,请往你的twenty ten主题的functions.php文件添加下列代码:
- add_action('generate_rewrite_rules', 'ashu_rewrite_rules' );
- /**********重写规则************/
- function ashu_rewrite_rules( $wp_rewrite ){
- $new_rules = array(
- 'my-account/?$' => 'index.php?my_custom_page=hello_page',
- ); //添加翻译规则
- $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
- //php数组相加
- }
前面我们关于过滤器的教程中提到了过滤器的用法及用处,重写规则位于一个数组中,我们使用过滤器钩子generate_rewrite_rules来更改这个规则数组,往里面添加内容,即达到了我们的目的。这个时候,我们访问地址(作者本机测试地址):localhost/newtheme/my-account/就会被翻译成index.php?my_custom_page=hello_page。
翻译规则介绍:
- 'my-account/?$' => 'index.php?my_custom_page=hello_page',
- /*
- 前面应该是一个正则表达式,my-account/?$ 只能匹配my-account/ 如果你访问的地址是localhost/newtheme/my-account/aa 则不能匹配
- */
注意到翻译后的地址中有一个my_custom_page,以及我们上一篇教程中列出来的author_name,这想当于一个变量。比如我们访问index.php?author_name=admin,通过这个变量的判断,载入wordpress的作者模板,然后根据这个变量的值admin,显示用户名为admin的内容。实际上这些变量存储在一个全局变量$public_query_vars,这是一个数组,只有数组中存在的变量才能被正确翻译,所以我们要往这个变量中添加元素。
二、添加$public_query_vars
- /*******添加query_var变量***************/
- add_action('query_vars', 'ashu_add_query_vars');
- function ashu_add_query_vars($public_query_vars){
- $public_query_vars[] = 'my_custom_page'; //往数组中添加添加my_custom_page
- return $public_query_vars;
- }
三、添加模板载入规则。
- //模板载入规则
- add_action("template_redirect", 'ashu_template_redirect');
- function ashu_template_redirect(){
- global $wp;
- global $wp_query, $wp_rewrite;
- //查询my_custom_page变量
- $reditect_page = $wp_query->query_vars['my_custom_page'];
- //如果my_custom_page等于hello_page,则载入user/helloashu.php页面
- //注意 my-account/被翻译成index.php?my_custom_page=hello_page了。
- if ($reditect_page == "hello_page"){
- include(TEMPLATEPATH.'/user/helloashu.php');
- die();
- }
- }
不要高兴的太早,到了这里还没完成呢,现在只添加了代码,但是重写规则还没存储到数据库。
四、更新重写规则
- /***************激活主题更新重写规则***********************/
- add_action( 'load-themes.php', 'frosty_flush_rewrite_rules' );
- function frosty_flush_rewrite_rules() {
- global $pagenow, $wp_rewrite;
- if ( 'themes.php' == $pagenow && isset( $_GET['activated'] ) )
- $wp_rewrite->flush_rules();
- }
OK,到了这里,到后台重新激活你的主题,然后访问地址: 你的 网址my-account/ 就能看到前面图示的效果了。
网上的所有重写规则方法都是返回404,我的扣扣46671143,希望有人能指导一下啦,不胜感激!
文章很多想分页,例如第二页,默认的格式是某某目录/page/2,想改成某某目录/page-2.html,试过网上N种方法,最后都是返回404,实在搞不懂了,能指导一下吗?心里很着急!谢谢老大了!
function ashu_rewrite_rules( $wp_rewrite ){
$new_rules = array(
‘play/(.*)?.html$’ => ‘index.php?my_page=play&path=$matches[1]’,
‘list/(.*)([\s\S]*)?$’ => ‘index.php?my_page=list&dir=$matches[1]’,
‘m3u8/(.*).m3u8([\s\S]*)’ => ‘index.php?my_page=m3u8&path=$matches[1]’,
‘thumbnail/(.*).jpg([\s\S]*)’ => ‘index.php?my_page=thumbnail&path=$matches[1]’,
‘ts/(.*).jpg([\s\S]*)’ => ‘index.php?my_page=ts&path=$matches[1]’,
); //添加翻译规则
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
//php数组相加
}
这样添加后页面后缀会自动添加/斜杠。有什么办法能去掉这个斜杠呢
太棒了!! ❓
博主你好,我想问一下你的第一步新建了一个php文件,然后访问 localhost/newtheme/my-account 就指向了你新建的PHP文件,他是怎么指向过去的呢?
我在网上找了好久都没有找到这相关的东西.
不好意思,原来你的代码已经有了-.-…谢谢博主..我刚好用到这个~
您好,我重写了文章页面的URL,’article/([0-9]+)?.html$’ => ‘index.php?p=$matches[1]’, 后台固定链接设置为:/%post_id%.html 这样,即使不输入article也能访问文章,一个页面就有两个URL了,如何实现不带article的自动301呢?
额?直接在后台设置固定连接为 article/%post_id%.html不就可以了吗?何必还来这个。。。
我现在用
function custom_post_rules() {
global $wp_rewrite;
$wp_rewrite->permalink_structure = $wp_rewrite->root . ‘article/%post_id%.html’;
}
add_action( ‘init’, ‘custom_post_rules’ );
没有发现我说的问题了
直接这样设置的话,作者页、日期归档页也会有article
add_action(‘generate_rewrite_rules’, ‘ashu_rewrite_rules’ );
function ashu_rewrite_rules( $wp_rewrite ){
$new_rules = array(
‘item/([0-9]+)?.html$’ => ‘index.php?my_custom_page=item&ids=$matches[1]’,
); //添加翻译规则
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}
后面的哪个ids 如何在自已添加的item.php面页中得到呀?
//模板载入规则
add_action(“template_redirect”, ‘ashu_template_redirect’);
function ashu_template_redirect(){
global $wp;
global $wp_query, $wp_rewrite;
//查询my_custom_page变量
$reditect_page = $wp_query->query_vars[‘my_custom_page’];
//如果my_custom_page等于hello_page,则载入user/helloashu.php页面
//注意 my-account/被翻译成index.php?my_custom_page=hello_page了。
if ($reditect_page == “item”){
require(TEMPLATEPATH.’/item.php’);
die();
}
}
用add_rewrite_rule()是不是方便些?
树哥,在function.php中怎样实现添加页面的功能?
我试着这样加了,但是不能实现,求树哥指导一下。
不要使用init钩子。。这个钩子是每次运行程序的时候执行,你这样这样岂不是每次运行都添加页面,建议使用本文中更新重写规则用到的钩子,新安装启用主题的时候执行。
改成
用这个
博主,为什么我按你的做法做,访问不到?求博主指导……
你是怎么个错误法呢?
请博主指导 在此先谢谢博主.
就和上面的例子一模一样,但是访问是404错误…… 我看了前面那篇博文 试了那个
这句执行结果是Array[0]
在function.php里面中加了断点,发现运行始终没有进ashu_rewrite_rules这个方法……
我在另的页面上运行这个:
直接报错:Unsupported operand types
是不是使用默认固定链接的原因?
你有尝试重新激活主题吗
嗯 都试过了……
因为你笨啊,
博主你好,为什么我按照你的做法下来,访问不到?
在文件里引入include_once(‘wp-config.php’);文件不就行了,不用那么复杂吧,不过可以学到新东西
引入wp-config.php文件怎么用?如果您有更好的代码还请指教。。。
引用配置文件就不必把文件放在主题里了,其他做法一样
我觉得放主题里面更方便,客户只需安装主题一个步骤即可。。。
我的站点也弄了个会员中心,测试帐号admin 密码:123456 你可以看看,其实我那个会员中心就是建一个单页面,这样也不用去解析这些url了吧,不用那么麻烦
嗯。。。用页面模板能行。。。