php - Mix two PHP Functions together
问题描述
Just looking on some assistance to see if the two functions below are able to be mixed together to clean up my code a little more:
/**
* Remove 'Posts Table' submenu from Admin Panel/Settings for all except ID: 4
*/
function pt_remove_admin_submenus() {
$currentUserId = get_current_user_id();
if ($currentUserId != 4) {
remove_submenu_page('options-general.php', 'posts_table');
}
}
add_action( 'admin_menu', 'pt_remove_admin_submenus', 999);
/**
* Remove 'JGC' submenu from Admin Panel/Settings for all except ID: 4
*/
function jgc_remove_admin_submenus() {
$currentUserId = get_current_user_id();
if ($currentUserId != 4) {
remove_submenu_page('options-general.php', 'jgccfr_settings');
}
}
add_action( 'admin_menu', 'jgc_remove_admin_submenus', 999);
解决方案
You could combine them to be in the same function like this:
function my_custom_remove_admin_submenus() {
if (get_current_user_id() !== 4) {
remove_submenu_page('options-general.php', 'posts_table');
remove_submenu_page('options-general.php', 'jgccfr_settings');
}
}
add_action('admin_menu', 'my_custom_remove_admin_submenus', 999);
I've also removed the variable that stored the current user id, and used that directly within the if()
.
推荐阅读
- angular - 使用特定类型的数据源创建材料表
- reactjs - React 项目与 react-router-dom 问题与 gh-pages 上的路由
- c - 查找最长公共子串问题的最小空间复杂度是多少?
- html - 具有各种向外和向内曲线的导航栏
- r - 如何从 R 中的外部函数创建稀疏矩阵?
- c++ - 将 void* 转换回数组
- mongodb - 使用 Mongoose 链接 2 个集合
- android - Android - 按下任何项目时如何取消BottomNavigationView动画
- javascript - toLocaleTimeString() 始终显示前导零
- c# - 单元测试图 SDK PageIterator