How to set Active plugins as the default screen?

In my testing environment, I’ve got one hundred plugins. I’m more interested in seeing first the Active plugins than the complete list (All).

How can I change the default screen for the Plugins menu item?

enter image description here

1 Answer
1

There are two options, modify the menu link href attribute or redirect the main screen.

Modify menu

Alter the global variable $submenu. This method works for Single and Multisite.
Problem: the submenu item Installed Plugins doesn’t show in bold.

add_action( 'admin_head', 'b5f_plugins_redirect_to_active' );

function b5f_plugins_redirect_to_active() 
{
    global $submenu;
    $submenu['plugins.php'][5][2] .= "?plugin_status=active";
    return;
}

Redirect page

Hook into load-plugins.php. Multisite detection is needed, but there are no UI problems.

add_action( 'load-plugins.php', 'b5f_ms_plugins_redirect_to_active' );

function b5f_ms_plugins_redirect_to_active() 
{
    // Trick, plugin_status is only false in WP default screen
    // ['action'] prevents us interfering with activation/deactivation 
    if( isset( $_GET['plugin_status'] ) || isset( $_GET['action'] ) )
        return;

    // Check for MS dashboard, redirect accordingly
    if( is_network_admin() )
        wp_redirect( network_admin_url( 'plugins.php?plugin_status=active' ) );
    else
        wp_redirect( admin_url( 'plugins.php?plugin_status=active' ) );

    exit();
}


And a couple of extras:

1) Rename the main link to Active

add_action( 'admin_menu', 'rename_submenu_item', 99 );

function rename_submenu_item() 
{
    global $submenu;
    foreach( $submenu['plugins.php'] as $key => $value )
    {
        if( in_array( 'activate_plugins', $value ) ) 
            $submenu['plugins.php'][$key][0] = __( 'Active' );
    }
}

2) Add a submenu to access All

The submenu link is hacked, as well as some jQuery printed to fix the submenus active state:

add_action( 'admin_menu', 'menu_admin_wpse_44270' );
add_action( 'admin_head-plugins.php', 'highlight_menu_item_wpse_44270' );

function menu_admin_wpse_44270() 
{
    add_submenu_page(
        'plugins.php',
        'Drafts', 
        '<span id="my-all-plugins">All</span>', 
        'edit_pages', 
        'plugins.php?plugin_status=all'
    );
}

function highlight_menu_item_wpse_44270()
{
    if( isset( $_GET['plugin_status'] ) && 'all' == $_GET['plugin_status'] )
    {       
        ?>
        <script type="text/javascript">
            jQuery(document).ready( function($) 
            {
                var reference = $('#my-all-plugins').parent().parent();
                // add highlighting to our custom submenu
                reference.addClass('current');
                //remove higlighting from the default menu
                reference.parent().find('li:nth-child(2)').removeClass('current');             
            });     
        </script>
        <?php
    }
}

Leave a Comment