根据条件添加管理菜单或子菜单

时间:2015-08-26 作者:Baylock

我在两个不同的文件中有两个插件,它们在管理面板中添加了一个子菜单(插件标题),如下所示:

插件1:

function myplugin1_setup_submenu(){
    add_submenu_page("mybrand", "My Plugin1", "My Plugin1", 0, "myplugin1", "myPlugin1PageFunction");
}
add_action(\'admin_menu\', \'myplugin1_setup_submenu\');
插件2:

function myplugin2_setup_submenu(){
    add_submenu_page("mybrand", "My Plugin2", "My Plugin2", 0, "myplugin2", "myPlugin2PageFunction");
}
add_action(\'admin_menu\', \'myplugin2_setup_submenu\');
必须在某个地方有一个菜单,它是这两个插件的品牌:

function mybrand_setup_menu(){
    add_menu_page( \'My Brand\', \'My Brand\', \'manage_options\', \'mybrand\', \'myBrandPageFunction\' );
}
add_action(\'admin_menu\', \'mybrand_setup_menu\');
但这不符合我的需要,因为我需要做的是:

如果该品牌安装了一个插件,我希望该插件是管理面板上的一个菜单(而不是子菜单),在这种情况下,我不希望该品牌出现在任何地方。

但是如果这个品牌安装了多个插件,我希望它们都是“我的品牌”菜单的子菜单。

我怎样才能做到这一点?

非常感谢。

1 个回复
SO网友:Baylock

我设法找到了一个解决方案:

文件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>";
    } 
我不知道这是否可以用更短的方式实现,但它确实有效。

相关推荐