我们在使用WordPress开发网站的过程中,很多时候需要修改网站后台的显示和布局。第一步就是修改仪表盘上的小工具。在前面的教程里我们已经向大家讲解了如何在仪表盘里添加小工具区块。在这篇教程里,我们将重点向大家讲解一下移除后台仪表盘上小工具区块的方法,移除的小工具区块见下图。
切换到主题目录,打开functions.php文件,加入以下代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <?php function remove_dashboard_widgets() { global $wp_meta_boxes; //Right Now - Comments, Posts, Pages at a glance unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_right_now']); //Recent Comments unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_recent_comments']); //Incoming Links unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_incoming_links']); //Plugins - Popular, New and Recently updated WordPress Plugins unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_plugins']); //Wordpress Development Blog Feed unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_primary']); //Other WordPress News Feed unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_secondary']); //Quick Press Form unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_quick_press']); //Recent Drafts List unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_recent_drafts']); //activity unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_activity']); } add_action('wp_dashboard_setup', 'remove_dashboard_widgets'); ?> |
上面的代码会把后台仪表盘上的所有默认小工具区块都删除,如果您想保留哪一个小工具区块,可以注释掉对应的代码。
如果我们只希望管理员角色可以看到这些小工具区块信息,其他用户角色(编辑、作者、投稿者、订阅者)看不到这些小工具区块信息,可以修改上面的代码为:
1 2 3 4 5 | <?php if (!current_user_can('manage_options')) { add_action('wp_dashboard_setup', 'remove_dashboard_widgets' ); } ?> |