我正在使用我的functions.php
在我的帖子頁面的WordPress管理區添加一個自定義元框。但是,我需要使其成爲唯一可見的管理員,而不是編輯,貢獻者等。如何讓我的自定義WordPress元框僅對管理員可見?
我該怎麼做才能使其僅顯示給管理員?
我正在使用我的functions.php
在我的帖子頁面的WordPress管理區添加一個自定義元框。但是,我需要使其成爲唯一可見的管理員,而不是編輯,貢獻者等。如何讓我的自定義WordPress元框僅對管理員可見?
我該怎麼做才能使其僅顯示給管理員?
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');
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
}
}
這段代碼適用於自定義分類。它爲所有非管理員移除/隱藏自定義分類學元框,假設沒有其他角色具有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
因爲WP 2.0 Userlevels已過時 - 他們仍然支持向後兼容性,但應該使用角色系統。 – 2011-05-24 22:05:21
@Johannes Pille:我沒有意識到用戶級別已被棄用這麼久!謝謝你提到這一點。 – smekosh 2011-05-25 18:12:19
他們畢竟工作。沒有必要重寫舊功能。但是,在編寫新的代碼時,繼續使用用戶級別是沒有意義的。這不像你的答案是錯的。只是老。 – 2011-05-25 21:56:09