哪一项修改为仅登录特定角色?

时间:2013-08-20 作者:Derfder

我已经在WP admin中创建了一个特殊角色,现在我想只允许具有此角色的人登录到某些前端页面。如何做到这一点?我仍然希望能够登录其他地区的所有人。

3 个回复
最合适的回答,由SO网友:Geert 整理而成

精心设计的插件可以根据用户角色或其他设置限制页面访问。但是,我想下面的代码很简单,可以检查用户角色。

<?php
global $current_user;
if ( is_page( \'some-page\' ) && in_array( \'some-role\', $current_user->roles ) ) {
    // Show the page
} else {
    // Howdy, stranger! Nothing to see here.
}
?>
注意:此代码可以进入主题的模板文件,例如index.phppage.php. 请注意,切换主题将删除用户角色访问限制。解决方案将此功能移动到插件。更新时间:G. M. provided a plugin version in his answer.

SO网友:s_ha_dum

您要使用user_can 检查用户角色。

$current_user = wp_get_current_user();
if (user_can( $current_user, \'specialrole\' )) {
  // user is in your role
}
很难说清楚你想做什么。您的问题非常少,不存在这样的问题;登录到页面;(不考虑受密码保护的页面,这些页面不根据角色进行操作)。

您可能只想将上述内容编辑成模板并打印一个;您没有访问权限消息“;对于没有正确角色的人。也许您想重定向到某个地方:

function redir_wrong_role() {
  if (is_page(\'Sample Page\')) {
    if (!user_can( $current_user, \'specialrole\' )) {
      wp_safe_redirect(home_url());
    }
  }
}
add_action(\'template_redirect\',\'redir_wrong_role\');
另请参见:
http://codex.wordpress.org/Function_Reference/wp_get_current_user
http://codex.wordpress.org/Function_Reference/is_page
http://codex.wordpress.org/Function_Reference/wp_safe_redirect

SO网友:gmazzap

第一个问题是如何识别页面是否是受限页面之一。

如果页面只有一个,您可以从我这里对其进行硬编码(即,自己写ID或slug),这并不好,但一个页面可以接受。如果页面不止一个,您可能需要找到不同的解决方案。

我建议创建一个名为“Restricted page”的页面模板,并将其分配给所有要限制的页面。

所以创建一个名为page-restriced.php, 将其保存在您的主题根目录中,并在其中编写如下内容:

<?php
/**
 * Template Name: Restricted Page
 */
?>

<?php get_template_part(\'page\'); ?>
现在,将“受限页面”模板分配给所有要重新限制的页面。

如果一个不允许的用户试图访问一个受限制的页面,您不会说要做什么。

一种解决方案是将页面的内容挂接到the_content 过滤器:

add_filter(\'the_content\', \'change_content_by_role\');

function change_content_by_role( $content ) {
  remove_filter(\'the_content\', \'change_content_by_role\');
  if ( is_page_template(\'page-restriced.php\') ) {
    // I suggest to allow access to administrator or probably you cannot access
    $allowed_roles = array(\'administrator\', \'your_special_role\'); 
    $the_user = wp_get_current_user();
    if ( empty( array_intersect($allowed_roles, $the_user->roles ) ) ) {
      return __(\'You are not allowed to view this page.\');
    }
  }
  return $content ;
}
这个解决方案很不错,但在某些特定情况下可能会带来问题:如果页面中有一个在主查询之前运行的辅助查询。

另一个无问题的解决方案是创建一个名为“不允许”的文件。php’并将其放在主题文件夹中,挂接到template_include 如果不允许用户,则筛选并显示该文件。

add_filter(\'template_include\', \'restrict_page_by_role\');

function restrict_page_by_role( $template ) {
  if ( is_page_template(\'page-restriced.php\') ) {
    // I suggest to allow access to administrator or probably you cannot access
    $allowed_roles = array(\'administrator\', \'your_special_role\'); 
    $the_user = wp_get_current_user();
    if ( empty( array_intersect($allowed_roles, $the_user->roles ) ) ) {
      return get_stylesheet_directory() . \'/not-allowed.php\';
    }
  }
  return $template;
}

结束

相关推荐

Login Button CSS

我正在尝试查找登录按钮CSS代码。我尝试过这个话题,但我认为在wordpress更新后,路径发生了变化:http://wordpress.org/support/topic/how-to-change-the-login-button-color有人能告诉我它在哪里吗?非常感谢。