2013-02-06 60 views
0

我有我寫的名爲MyPlugin的插件。我有一個是或否的單選按鈕,如果選項是肯定的意味着它應該允許貢獻者上傳圖像,並不意味着它不應該允許。如何在我的插件中添加admin_init,以便貢獻者可以上傳圖片

這是代碼,允許貢獻者上傳圖片

if (current_user_can('contributor') && !current_user_can('upload_files')) 
    add_action('admin_init', 'allow_contributor_uploads'); 
function allow_contributor_uploads() { 
    $contributor = get_role('contributor'); 
    $contributor->add_cap('upload_files'); 
} 
// this is the code to remove if the capabilities if it is added 

if (current_user_can('contributor') && current_user_can('upload_files')) 
    add_action('admin_init', 'remove_contributor_upload'); 
function remove_contributor_upload(){ 
    $con = get_role('contributor'); 
    $con->remove_cap('upload_files'); 
} 

我需要幫助,我應該在哪裏把這個代碼的插件,我嘗試過,但我得到了錯誤的

Error in wp-includes/capabilities.php on line 1059 
+0

什麼是錯誤,這是什麼版本的WordPress? – webaware

+0

Wordpress版本是3.0.1,錯誤是調用未定義的函數wp_get_current_user() –

+0

爲什麼你運行這樣一箇舊版本的WordPress? – webaware

回答

1

您正在調用init()之前無法調用的函數,因此它們未定義(具體來說,wp_get_current_user()是在wp-includes/pluggable.php中定義的,並且在加載所有插件之後才加載)。在調用 admin_init後,您需要重新排列代碼以檢查用戶權限

add_action('admin_init', 'allow_contributor_uploads'); 
function allow_contributor_uploads() { 
    if (current_user_can('contributor') && !current_user_can('upload_files')) { 
     $contributor = get_role('contributor'); 
     $contributor->add_cap('upload_files'); 
    } 
} 
// this is the code to remove if the capabilities if it is added 

add_action('admin_init', 'remove_contributor_upload'); 
function remove_contributor_upload(){ 
    if (current_user_can('contributor') && current_user_can('upload_files')) { 
     $con = get_role('contributor'); 
     $con->remove_cap('upload_files'); 
    } 
} 

注:只是在這裏解決你的錯誤問題,沒有看過你在做什麼的邏輯!

+0

@webware這兩個代碼是爲了允許和防止貢獻者上傳圖片! –