最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Redirect Admin User in Dashboard

programmeradmin2浏览0评论

I am currently trying to setup a redirect so that my admin users are redirected to a page other than the dashboard within the wordpress administrator interface.

If I leave out my conditional, the redirect works, but then it also redirects non-administrator users as well and I don't want this.

Here is the code I have within functions.php

add_filter('login_redirect', 'dashboard_redirect');
function dashboard_redirect($url) {
  global $current_user;
  get_currentuserinfo();
  $level = (int) $current_user->wp_user_level;

  if ( $level > 10  ) {
    $url = 'wp-admin/edit.php';
  }

  return $url;
}     

I am currently trying to setup a redirect so that my admin users are redirected to a page other than the dashboard within the wordpress administrator interface.

If I leave out my conditional, the redirect works, but then it also redirects non-administrator users as well and I don't want this.

Here is the code I have within functions.php

add_filter('login_redirect', 'dashboard_redirect');
function dashboard_redirect($url) {
  global $current_user;
  get_currentuserinfo();
  $level = (int) $current_user->wp_user_level;

  if ( $level > 10  ) {
    $url = 'wp-admin/edit.php';
  }

  return $url;
}     
Share Improve this question asked Nov 9, 2011 at 19:02 elkefreedelkefreed 3851 gold badge5 silver badges16 bronze badges 1
  • 1 How about accepting answers that certainly saved you time as correct? – Johannes P. Commented Nov 25, 2011 at 23:47
Add a comment  | 

4 Answers 4

Reset to default 7

You should not use Userlevels. Userlevels have been replaced in WP 2.0 and have been officially deprecated since 3.0

add_filter( 'login_redirect', 'dashboard_redirect' );
function dashboard_redirect( $url ) {
    if ( current_user_can( 'manage_options' ) ) {
         $url = esc_url( admin_url( 'edit.php' ) );
    }

    return $url;
}    

Will do what you want.

If you want to redirect to another page any time they try to access the dashboard, and not just after login, use something like this:

add_action( 'current_screen', function() {
    $screen = get_current_screen();
    if ( isset( $screen->id ) && $screen->id == 'dashboard' ) {
        wp_redirect( admin_url( 'edit.php?post_type=my-post-type' ) );
        exit();
    }
} );

Yan also add this simple action to the 'login_form' (see this site for more detail).
For example, to redirect to dashboard, you can use:

add_action('login_form', 'redirect_after_login');
function redirect_after_login() {
    global $redirect_to;
    if (!isset($_GET['redirect_to'])) {
        $redirect_to = get_option('siteurl') . '/wp-admin/index.php';
    }
}

Try wrapping the function with this current_user condition:

if (current_user_can('administrator')) {
// Your Redirect Code Here
}

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论