我设法找到了一个解决方案:
文件1:
//Count the number of active plugins whose Author is "My Brand", and store the number in a global variable.
$GLOBALS[\'mybrand_active_plugins\']=0;
if (!function_exists(\'get_plugins\')){require_once ABSPATH.\'wp-admin/includes/plugin.php\';}
$plugins=get_plugins();
$activated_plugins=get_option(\'active_plugins\');
foreach ($activated_plugins as $p){
if( (isset($plugins[$p])) AND ($plugins[$p][\'Author\']==\'My Brand\')){
$GLOBALS[\'mybrand_active_plugins\']++;
}
}
// Add menus on sidebar
if($GLOBALS[\'mybrand_active_plugins\']<2)
{
function sub1_setup_menu()
{
add_menu_page(\'Sub 1\',\'Sub 1\',\'manage_options\',\'sub1\',\'sub1_admin_page\');
}
add_action(\'admin_menu\',\'sub1_setup_menu\');
}
else
{
function sub1_setup_menu()
{
if (empty($GLOBALS[\'admin_page_hooks\'][\'My Brand\'] ) ){
add_menu_page(\'My Brand\',\'My Brand\',\'manage_options\',\'mybrand\',\'mybrand1_admin_page\');
}
add_submenu_page("mybrand","Sub 1","Sub 1",\'manage_options\',"sub1","sub1_admin_page");
remove_submenu_page("mybrand","mybrand");
}
add_action(\'admin_menu\',\'sub1_setup_menu\');
}
// Initialize Brand and Plugin Admin Page
function mybrand1_admin_page()
{
echo "<h1>My Brand Plugins</h1>";
}
function sub1_admin_page()
{
echo "<h1>Sub 1</h1>";
}
文件2:
//Count the number of active plugins whose Author is "My Brand", and store the number in a global variable.
$GLOBALS[\'mybrand_active_plugins\']=0;
if (!function_exists(\'get_plugins\')){require_once ABSPATH.\'wp-admin/includes/plugin.php\';}
$plugins=get_plugins();
$activated_plugins=get_option(\'active_plugins\');
foreach ($activated_plugins as $p){
if( (isset($plugins[$p])) AND ($plugins[$p][\'Author\']==\'My Brand\')){
$GLOBALS[\'mybrand_active_plugins\']++;
}
}
// Add menus on sidebar
if($GLOBALS[\'mybrand_active_plugins\']<2)
{
function sub2_setup_menu()
{
add_menu_page(\'Sub 2\',\'Sub 2\',\'manage_options\',\'sub2\',\'sub2_admin_page\');
}
add_action(\'admin_menu\',\'sub2_setup_menu\');
}
else
{
function sub2_setup_menu()
{
if (empty($GLOBALS[\'admin_page_hooks\'][\'My Brand\'])){
add_menu_page(\'My Brand\',\'My Brand\',\'manage_options\',\'mybrand\',\'mybrand2_admin_page\');
}
add_submenu_page("mybrand","Sub 2","Sub 2",\'manage_options\',"sub2","sub2_admin_page");
remove_submenu_page("mybrand","mybrand");
}
add_action(\'admin_menu\',\'sub2_setup_menu\');
}
// Initialize Brand and Plugin Admin Page
function mybrand2_admin_page()
{
echo "<h1>My Brand Plugins</h1>";
}
function sub2_admin_page()
{
echo "<h1>Sub 2</h1>";
}
我不知道这是否可以用更短的方式实现,但它确实有效。