我想有条件地呈现一些自定义仪表板小部件。如果登录用户的角色大于或等于编辑器角色,我希望呈现比具有订阅者角色的用户更多的小部件。如何做到这一点?任何帮助请。。。
如何在WordPress管理中有条件地呈现仪表板小工具
1 个回复
最合适的回答,由SO网友:EarnestoDev 整理而成
// PHP 5.3 syntax - anonymous functions (Closures)
add_action(\'wp_dashboard_setup\', function(){
if(current_user_can(\'activate_plugins\')){ // Administrator
wp_add_dashboard_widget(\'dbwidget-Administrator\', \'#Administrator\', function(){
// Print widget here
});
}elseif(current_user_can(\'delete_others_posts\')){ // Editor
wp_add_dashboard_widget(\'dbwidget-Editor\', \'#Editor\', function(){
// Print widget here
});
}elseif(current_user_can(\'delete_published_posts\')){ // Author
wp_add_dashboard_widget(\'dbwidget-Author\', \'#Author\', function(){
// Print widget here
});
}elseif(current_user_can(\'edit_posts\')){ // Contributor
wp_add_dashboard_widget(\'dbwidget-Contributor\', \'#Contributor\', function(){
// Print widget here
});
}elseif(current_user_can(\'read\')){ // Subscriber
wp_add_dashboard_widget(\'dbwidget-Subscriber\', \'#Subscriber\', function(){
// Print widget here
});
}
});
正如您所见,我将它们与elseif链接起来,以确保角色顺利降级,因为管理员也可以完成以下所有操作。随心所欲地玩吧。了解更多有关Roles and Capabilities here.结束