0

I have two plugins in two different files that add a submenu (the plugin title) to the admin panel like this:

Plugin 1:

function myplugin1_setup_submenu(){
    add_submenu_page("mybrand", "My Plugin1", "My Plugin1", 0, "myplugin1", "myPlugin1PageFunction");
}
add_action('admin_menu', 'myplugin1_setup_submenu');

Plugin 2:

function myplugin2_setup_submenu(){
    add_submenu_page("mybrand", "My Plugin2", "My Plugin2", 0, "myplugin2", "myPlugin2PageFunction");
}
add_action('admin_menu', 'myplugin2_setup_submenu');

There must be, somewhere, a menu which is the brand of these 2 plugins:

function mybrand_setup_menu(){
    add_menu_page( 'My Brand', 'My Brand', 'manage_options', 'mybrand', 'myBrandPageFunction' );
}
add_action('admin_menu', 'mybrand_setup_menu');

But this will not suit my needs because what I need to do is this:

  • If a single plugin is installed from this brand, I would like this plugin to be a menu (not a submenu) on the admin panel and, in this case, I don't want the brand to appear anywhere.

  • But if there are more than one plugin installed from this brand, I would like
    them all to be submenus of the "My Brand" menu.

How can I achieve that?

Thank you.

  • 1
    First things first: How are you determining whether one or more plugins are installed from "this brand"? – DrewAPicture Aug 26 '15 at 7:07
  • I believe it is part of the issue. It can be retrieved from the "Author" in the files header or It can be a constant variable I add on each plugin which will contain the brand name or it can be the first argument from "add_submenu_page" which is always the brand slug. I don't know if all or none of these "flags" are usable to detect the plugin as belonging to the brand. Thank you for your help. – Baylock Aug 26 '15 at 13:38
  • The good news, is that you can do it all in the same hook. – DrewAPicture Aug 26 '15 at 15:52
  • Your answer is more teasing than helping. Anyway, I found a way to do it. Don't know if it is optimized but at least, it works. Thank you. – Baylock Aug 26 '15 at 16:05
  • I started to write an answer this morning and got distracted. Let me see if I can find the tab and finish it out. :) – DrewAPicture Aug 26 '15 at 21:32
0

I managed to find a solution:

File 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>";
    } 

File 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>";
    } 

I don't know if this can be achieved in a shorter way but it works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.