1. Ẩn menu không cần thiết theo vai trò
add_action('admin_menu', function () {
if (!current_user_can('administrator')) {
remove_menu_page('tools.php'); // Công cụ
remove_menu_page('edit-comments.php'); // Bình luận
remove_menu_page('plugins.php'); // Plugin
remove_menu_page('themes.php'); // Giao diện
}
});
Thay !current_user_can('administrator') bằng kiểm tra vai trò cụ thể nếu cần.
2. Ẩn thanh quản trị (admin bar) trên frontend
add_filter('show_admin_bar', function ($show) {
if (!current_user_can('edit_posts')) {
return false;
}
return $show;
});
3. Tùy chỉnh dashboard cho từng vai trò
add_action('wp_dashboard_setup', function () {
if (current_user_can('contributor')) {
remove_meta_box('dashboard_quick_press', 'dashboard', 'side');
remove_meta_box('dashboard_primary', 'dashboard', 'side');
remove_meta_box('dashboard_activity', 'dashboard', 'normal');
}
});
4. Thêm thông báo hoặc hướng dẫn riêng
add_action('admin_notices', function () {
if (current_user_can('author')) {
echo '<div class="notice notice-info"><p>Bạn chỉ cần vào mục "Bài viết" để viết và quản lý nội dung.</p></div>';
}
});
5. Dùng CSS để ẩn giao diện không cần thiết
add_action('admin_head', function () {
if (current_user_can('subscriber')) {
echo '<style>
#menu-dashboard, #menu-media, #menu-comments {
display: none !important;
}
</style>';
}
});
6. Đổi logo admin theo vai trò
add_action('admin_head', function () {
if (current_user_can('editor')) {
echo '<style>
#wp-admin-bar-wp-logo {
background-image: url(/custom-logo-editor.png) !important;
background-size: contain !important;
}
</style>';
}
});
Kết luận
Việc tách giao diện admin theo vai trò người dùng giúp WordPress trở nên thân thiện hơn cho nhóm biên tập viên, cộng tác viên hoặc khách hàng không rành kỹ thuật.
Hãy kết hợp các đoạn code trên theo nhu cầu thực tế, và kiểm tra kỹ quyền hạn người dùng để đảm bảo an toàn và dễ dùng.
Bình luận