仅允许在wordpress中具有特定角色的特定用户访问两个(/ home,/ inbox)页面

时间:2016-09-02 15:12:52

标签: wordpress url-redirection

我想只给两个页面(/ home,/ inbox)访问我的用户角色“Vendor”,如果用户试图访问其他页面,它会自动重定向到“/ inbox”页面,我把下面的内容实现功能的代码,但在将此添加到function.php之后,网站再次n再次重定向,最后死于消息“页面未正确重定向”。请告知我尝试的代码或任何其他解决方案有什么问题。

function vendor_redirect() {
   global $post;
   if(current_user_can('Vendor') && !in_array($post->slug,array("home","inbox"))) {
      wp_safe_redirect('/inbox');
   }
}
add_action('template_redirect', 'vendor_redirect'); 

1 个答案:

答案 0 :(得分:0)

我尝试获取页面slug的主要问题是,获取slug的正确方法是" $ post-> post_name"。我也在wp_safe_redirect之后退出,因为codex建议:

function vendor_redirect() {
   global $post;
   if(current_user_can('Vendor') && !in_array($post->post_name,array("home","inbox"))) {
      wp_safe_redirect(get_permalink(get_page_by_path( 'inbox' )));
      exit;
   }
}
add_action('template_redirect', 'vendor_redirect');
相关问题