Сделайте WP metabox доступным для редактирования только администраторам

У меня есть сайт-каталог участников, и мне нужна помощь.

У меня есть таксономия, связанная с настраиваемым типом сообщения в Wordpress. Это позволяет мне применять к каждому сообщению географический регион. Проблема в том, что любой участник, имеющий доступ к любому заданному сообщению, также имеет доступ к обновлению региона. Я хотел бы сделать метабокс региона доступным для редактирования только для ролей администратора. Вот текущий код:

function region() {

$labels = array(
    'name'                       => _x( 'Regions', 'Taxonomy General Name', 'text_domain' ),
    'singular_name'              => _x( 'Region', 'Taxonomy Singular Name', 'text_domain' ),
    'menu_name'                  => __( 'Regions', 'text_domain' ),
    'all_items'                  => __( 'All Items', 'text_domain' ),
    'parent_item'                => __( 'Parent Item', 'text_domain' ),
    'parent_item_colon'          => __( 'Parent Item:', 'text_domain' ),
    'new_item_name'              => __( 'New Item Name', 'text_domain' ),
    'add_new_item'               => __( 'Add New Item', 'text_domain' ),
    'edit_item'                  => __( 'Edit Item', 'text_domain' ),
    'update_item'                => __( 'Update Item', 'text_domain' ),
    'view_item'                  => __( 'View Item', 'text_domain' ),
    'separate_items_with_commas' => __( 'Separate items with commas', 'text_domain' ),
    'add_or_remove_items'        => __( 'Add or remove items', 'text_domain' ),
    'choose_from_most_used'      => __( 'Choose from the most used', 'text_domain' ),
    'popular_items'              => __( 'Popular Items', 'text_domain' ),
    'search_items'               => __( 'Search Items', 'text_domain' ),
    'not_found'                  => __( 'Not Found', 'text_domain' ),
);
$args = array(
    'labels'                     => $labels,
    'hierarchical'               => true,
    'public'                     => true,
    'show_ui'                    => true,
    'show_admin_column'          => true,
    'map_meta_cap'               => false,      
    'show_in_nav_menus'          => true,
    'show_tagcloud'              => true,
);
register_taxonomy( 'region', array( 'installer' ), $args );

} add_action ('инициализация', 'регион', 0);

Я попытался изменить show_ui на false, но это также скрывает его от администраторов. Я знаю, что должно быть простое решение, но не нахожу его.


person InfernalRed    schedule 19.04.2017    source источник
comment
Что если вы сделаете параметр show_ui логической переменной и в верхней части функции выполните что-то вроде $show_ui = (current_user_can('administrator') ? true : false);   -  person Ty Bailey    schedule 19.04.2017


Ответы (1)


Спасибо, Тай, за то, что указал мне в правильном направлении. Я добавил оператор if / else, который, кажется, помогает. См. ниже:

function region() {

$labels = array(
    'name'                       => _x( 'Regions', 'Taxonomy General Name', 'text_domain' ),
    'singular_name'              => _x( 'Region', 'Taxonomy Singular Name', 'text_domain' ),
    'menu_name'                  => __( 'Regions', 'text_domain' ),
    'all_items'                  => __( 'All Items', 'text_domain' ),
    'parent_item'                => __( 'Parent Item', 'text_domain' ),
    'parent_item_colon'          => __( 'Parent Item:', 'text_domain' ),
    'new_item_name'              => __( 'New Item Name', 'text_domain' ),
    'add_new_item'               => __( 'Add New Item', 'text_domain' ),
    'edit_item'                  => __( 'Edit Item', 'text_domain' ),
    'update_item'                => __( 'Update Item', 'text_domain' ),
    'view_item'                  => __( 'View Item', 'text_domain' ),
    'separate_items_with_commas' => __( 'Separate items with commas', 'text_domain' ),
    'add_or_remove_items'        => __( 'Add or remove items', 'text_domain' ),
    'choose_from_most_used'      => __( 'Choose from the most used', 'text_domain' ),
    'popular_items'              => __( 'Popular Items', 'text_domain' ),
    'search_items'               => __( 'Search Items', 'text_domain' ),
    'not_found'                  => __( 'Not Found', 'text_domain' ),
);
if (current_user_can('administrator') ){
    $args = array(
    'labels'                     => $labels,
    'hierarchical'               => true,
    'public'                     => true,
    'show_ui'                    => true,
    'show_admin_column'          => true,
    'map_meta_cap'               => false,      
    'show_in_nav_menus'          => true,
    'show_tagcloud'              => true,
);
}else{
    $args = array(
    'labels'                     => $labels,
    'hierarchical'               => true,
    'public'                     => true,
    'show_ui'                    => false,
    'show_admin_column'          => true,
    'map_meta_cap'               => false,      
    'show_in_nav_menus'          => true,
    'show_tagcloud'              => true,
);
}
register_taxonomy( 'region', array( 'installer' ), $args );

}
add_action( 'init', 'region', 0 );

А затем я просто изменил show_ui_ с true на false в зависимости от роли администратора и неадминистратора. Если кто-то видит более простой способ сделать это, я бы с удовольствием его увидел.

person InfernalRed    schedule 20.04.2017