wordpress进阶教程(16):添加一个重写规则,构建新页面初试

前面一篇教程我们介绍了wordpress url重写是怎么工作的,这一篇教程我们来个小例子,加深对wordpress url重写的认识。

今天的例子要做到的(这是wordpress自定义会员系统的雏形哦)

以默认的twenty ten 主题为例,我们现在默认主题中新建一个user文件夹,文件夹中添加一个php文件,里面的代码为:

  1. <?php   
  2. /*********用户欢迎页面************/  
  3. get_header(); //载入头部文件   
  4.   
  5. if( !is_user_logged_in()) { //判断用户是否登录   
  6.     echo '<div style="text-align:center; margin:40px;">访客您好,请先登录。</div>';   
  7. }else{   
  8.     global $current_user;   
  9.     get_currentuserinfo();   
  10.     $uname = $current_user->user_nicename;   
  11.        
  12.     echo '<div style="text-align:center; margin:40px;"><span style="color:red;">';   
  13.     echo $uname//输出用户名   
  14.     echo '</span>您好,欢迎登陆...</div>';   
  15. }   
  16.   
  17. get_footer(); //载入底部文件   
  18. ?>  

这个文件通过判断用户分别输出一句欢迎语句。效果图(点击图片查看大图)

wordpress会员欢迎页面

实际操作:

一、添加翻译规则。首先我们前面介绍了url的翻译规则,我们要往翻译规则中添加一条自己的翻译规则,请往你的twenty ten主题的functions.php文件添加下列代码:

  1. add_action('generate_rewrite_rules', 'ashu_rewrite_rules' );   
  2. /**********重写规则************/  
  3. function ashu_rewrite_rules( $wp_rewrite ){   
  4.     $new_rules = array(    
  5.         'my-account/?$' => 'index.php?my_custom_page=hello_page',   
  6.     ); //添加翻译规则   
  7.     $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;   
  8.     //php数组相加   
  9. }  

前面我们关于过滤器的教程中提到了过滤器的用法及用处,重写规则位于一个数组中,我们使用过滤器钩子generate_rewrite_rules来更改这个规则数组,往里面添加内容,即达到了我们的目的。这个时候,我们访问地址(作者本机测试地址):localhost/newtheme/my-account/就会被翻译成index.php?my_custom_page=hello_page。
翻译规则介绍

  1. 'my-account/?$' => 'index.php?my_custom_page=hello_page',   
  2. /*
  3. 前面应该是一个正则表达式,my-account/?$ 只能匹配my-account/ 如果你访问的地址是localhost/newtheme/my-account/aa 则不能匹配  
  4. */  

注意到翻译后的地址中有一个my_custom_page,以及我们上一篇教程中列出来的author_name,这想当于一个变量。比如我们访问index.php?author_name=admin,通过这个变量的判断,载入wordpress的作者模板,然后根据这个变量的值admin,显示用户名为admin的内容。实际上这些变量存储在一个全局变量$public_query_vars,这是一个数组,只有数组中存在的变量才能被正确翻译,所以我们要往这个变量中添加元素。

二、添加$public_query_vars

  1. /*******添加query_var变量***************/  
  2. add_action('query_vars', 'ashu_add_query_vars');   
  3. function ashu_add_query_vars($public_query_vars){     
  4.     $public_query_vars[] = 'my_custom_page'; //往数组中添加添加my_custom_page   
  5.        
  6.     return $public_query_vars;     
  7. }  

三、添加模板载入规则。

  1. //模板载入规则   
  2. add_action("template_redirect", 'ashu_template_redirect');   
  3. function ashu_template_redirect(){   
  4.     global $wp;   
  5.     global $wp_query$wp_rewrite;   
  6.        
  7.     //查询my_custom_page变量   
  8.     $reditect_page =  $wp_query->query_vars['my_custom_page'];   
  9.     //如果my_custom_page等于hello_page,则载入user/helloashu.php页面   
  10.     //注意 my-account/被翻译成index.php?my_custom_page=hello_page了。   
  11.     if ($reditect_page == "hello_page"){   
  12.         include(TEMPLATEPATH.'/user/helloashu.php');   
  13.         die();   
  14.     }   
  15. }  

不要高兴的太早,到了这里还没完成呢,现在只添加了代码,但是重写规则还没存储到数据库。
四、更新重写规则

  1. /***************激活主题更新重写规则***********************/  
  2. add_action( 'load-themes.php', 'frosty_flush_rewrite_rules' );   
  3. function frosty_flush_rewrite_rules() {   
  4.     global $pagenow$wp_rewrite;   
  5.     if ( 'themes.php' == $pagenow && isset( $_GET['activated'] ) )   
  6.         $wp_rewrite->flush_rules();   
  7. }  

OK,到了这里,到后台重新激活你的主题,然后访问地址:   你的 网址my-account/   就能看到前面图示的效果了。

已有31条评论

helloworld进行回复 取消回复