根据用户角色进行WordPress登录重定向

时间:2012-01-31 作者:Ajay Patel

在我的应用程序中,用户需要1次登录,然后才能访问网站。

我的网站上有3个不同的用户角色

管理员、编辑、贡献者、用户可以从

http://example.com/wordpress/wp-login.php
没有从任何主题模板登录

我找到了这个代码,但它不起作用

function my_login_redirect_contributors() {
  if ( current_user_can(\'contributor\') ){
      return \'url-to-redirect-to\';
  }
}

add_filter(\'login_redirect\', \'my_login_redirect_contributors\');
注意:我不想使用像Peter的登录重定向这样的插件。我想通过定制带有过滤器的wp核心代码来实现它

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

您正在使用筛选器wrong. 如果你有不止一个@param, 然后,您必须在添加过滤器时声明它们。

function wpse40745_redirect_on_login( $redirect_to, isset( $_REQUEST[\'redirect_to\'] ), $user )
{
    # uncomment the following line if it\'s not working and replace $user->roles[0] with something that works
    // echo \'<pre>\'; print_r( $user ); echo \'</pre>\'; exit;

    if ( in_array( $user->roles[0], array( \'contributer\', \'whatever_other_role ) ) )
        return home_url();

    return $redirect_to;
}
add_filter( \'login_redirect\', \'wpse40745_redirect_on_login\', 20, 3 );

结束

相关推荐

WP_LOGIN_FORM:根据用户名重定向到动态URL

我需要在登录后重定向用户。我使用wp_login_form 在frontpage上提供登录表单。用户输入用户名和密码后,我想重定向到自定义url,如http://myurl.com/username/ 其中“username”是用户输入的名称。我能过滤一下吗login_redirect?非常感谢。