2012-11-15 33 views
3

我已經在functions.php中添加了一個函數,用於在登錄後將用戶重定向到posts-new.php並且它可以工作。不過,我只希望這種情況發生,如果用戶登錄是貢獻者。所以我添加了以下內容:function.php中的Wordpress功能和current_user_can()

/** Redirect after login */ 
    function mysite_login_redirect(){ 
     if (current_user_can('manage_options')) { 
      return 'http://mysite.com/wp-admin/index.php';} 
     else { 
      return 'http://mysite.com/wp-admin/post-new.php';} 
    } 
add_action('login_redirect', 'mysite_login_redirect'); 

在這種狀態下,貢獻者和管理員都被重定向到post-new.php。爲了測試它,我修改了功能,使用戶沒有能力將被重定向:

if (!current_user_can('ma ... 

當我修改的功能,無論是捐助者和管理員被重定向到的index.php。

因此,該功能似乎工作,但這意味着它沒有看到管理員的'manage_options'功能。我已經嘗試了幾個具有相同結果的管理專用功能。怪怪的?

我應該說我正在使用用戶角色編輯器插件,但我禁用了它並測試了具有相同結果的函數。

我還使用Active Directory集成和管理菜單編輯器。

+0

更多細節可以在這裏找到https://codex.wordpress.org/Function_Reference/current_user_can我 –

回答

10

試試這個:

if(current_user_can('administrator')){} // only if administrator 
if(current_user_can('editor')){} // only if editor 
if(current_user_can('author')){} // only if author 
if(current_user_can('contributor')){} // only if contributor 
if(current_user_can('subscriber')){} // only if subscriber 

或者:

if(current_user_can('level_10')){} 
if(current_user_can('level_9')){} 
if(current_user_can('level_8')){} 
if(current_user_can('level_7')){} 
if(current_user_can('level_6')){} 
if(current_user_can('level_5')){} 
if(current_user_can('level_4')){} 
if(current_user_can('level_3')){} 
if(current_user_can('level_2')){} 
if(current_user_can('level_1')){} 
if(current_user_can('level_0')){} 
+0

嘗試使用 '管理員',而不是'manage_options',但我得到了相同的結果。如果我理解正確,則current_user_can()標記僅適用於功能而不適用於用戶角色。 – ItsGeorge

+1

它的參數是一個能力或角色名稱,所以它應該工作(http://codex.wordpress.org/Function_Reference/current_user_can)。 –

+0

因此,無論哪種方式,該功能都不起作用,問題是功能無法正確識別角色和功能。如果我將其設置爲特定於管理員的角色,或者只是「管理員」,那麼如果我以管理員身份登錄,則應使用「if」網址;如果我以貢獻者身份登錄,則應使用「其他」網址。無論哪種方式,無論我將函數設置爲'current_user_can'還是'!current_user_can',管理員登錄名和參與者登錄名的行爲都與功能沒有任何區別。 – ItsGeorge