首页 > 解决方案 > 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);

标签: phpwordpress

解决方案


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().


推荐阅读