【问题标题】:Wordpress Functions Force Dashboard For All UsersWordPress 功能强制所有用户使用仪表板
【发布时间】:2013-10-18 19:28:38
【问题描述】:

我试图强制将 wordpress 仪表板部分作为所有用户的欢迎消息,但失败了。有没有人有一个功能可以在订阅者登录时强制使用仪表板?

// Redirect admin to the dashboard and other users elsewhere 
add_filter( 'login_redirect', 'my_login_redirect', 10, 3 ); 

function my_login_redirect( $redirect_to, $request, $user ) { 
    // Is there a user? 
    if ( is_array( $user->roles ) ) { 
        // Is it an administrator? 
        if ( in_array( 'administrator', $user->roles ) ) 
            return home_url( '/wp-admin/' ); 
        else 
            return home_url(); 
            // return get_permalink( 83 ); 
    } 
}

【问题讨论】:

  • 我一直在寻找这里并尝试了一些没有运气的功能。感谢 Brasofilo 抽出宝贵时间:codex.wordpress.org/…
  • // 将管理员重定向到仪表板和其他地方的其他用户 add_filter( 'login_redirect', 'my_login_redirect', 10, 3 ); function my_login_redirect( $redirect_to, $request, $user ) { // 有用户吗? if ( is_array( $user->roles ) ) { // 是管理员吗? if ( in_array( 'administrator', $user->roles ) ) return home_url( '/wp-admin/' );否则返回 home_url(); // 返回 get_permalink( 83 ); } }
  • 您可以随时edit 提问。此处的代码在 cmets 中不可读。
  • 我不知道如何让代码在这里可读

标签: php wordpress dashboard


【解决方案1】:

您必须检查是否设置了$user,然后检查它是否是一个数组。如果启用了 WP_DEBUG,这将阻止 PHP 通知。

然后,使用函数site_url()admin_url() 设置重定向:

add_filter( 'login_redirect', 'login_redirect_so_19305904', 10, 3 ); 

function login_redirect_so_19305904( $redirect_to, $request, $user ) 
{ 
    if ( isset( $user->roles ) && is_array( $user->roles ) ) 
    { 
        if ( in_array( 'administrator', $user->roles ) ) 
            return admin_url( 'edit.php' ); // Goes to example.com/wp-admin/edit.php
        else 
            return site_url( 'about' ); // Goes to example.com/about/
    } 
    return $redirect_to;
}

【讨论】:

  • 谢谢你看到上面我编辑了代码,它现在完美了,非常感谢。
  • 约翰,您不需要将解决方案添加到问题中。也许您应该阅读以下内容:How does accepting an answer work?
  • 我还不能投票,因为我是新手。看来我需要15分。再次感谢您的信息
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-16
  • 2012-05-27
  • 2011-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多