2011-05-24 51 views

回答

4
function your_function() { 
    global $current_user; 
    if($current_user->roles[0] == 'administrator') { 
     add_meta_box(your parameters); 
     // fill in your parameters 
    } 
} 
add_action('admin_init','your_function'); 
0
if (is_user_logged_in()) { 
    get_currentuserinfo(); 
    # check if current user is admin 
    if ($current_user->wp_user_level >= 10) { 
     # put your admin-only function here 
    } 
} 
+0

因爲WP 2.0 Userlevels已過時 - 他們仍然支持向後兼容性,但應該使用角色系統。 – 2011-05-24 22:05:21

+0

@Johannes Pille:我沒有意識到用戶級別已被棄用這麼久!謝謝你提到這一點。 – smekosh 2011-05-25 18:12:19

+0

他們畢竟工作。沒有必要重寫舊功能。但是,在編寫新的代碼時,繼續使用用戶級別是沒有意義的。這不像你的答案是錯的。只是老。 – 2011-05-25 21:56:09

0

這段代碼適用於自定義分類。它爲所有非管理員移除/隱藏自定義分類學元框,假設沒有其他角色具有update_core功能。類似的,但得到的答覆相反通過@約翰內斯-Pille先生

function remove_tax_metaboxes() { 
    if (!current_user_can('update_core')) { 
     remove_meta_box('taxdiv', 'post', 'side'); 
    } 
} 
add_action('do_meta_boxes', 'remove_tax_metaboxes'); 

注意的remove_meta_box第三個參數可能會有所不同,請參閱https://codex.wordpress.org/Function_Reference/remove_meta_box

相關問題