Over in a comment thread on the Tavern, Adam mentioned that he’d like to have a plugin count in the Right Now box on the Dashboard. As it happens, I once wrote a plugin for a client that added some information to the Right Now box, so it was easy to alter it to print the number of plugins:
<?php /* Plugin Name: How Many Plugins Plugin URI: http://stephanieleary.com/ Description: Counts the number of active plugins you have and prints a link in the Right Now box Author: Stephanie Leary Version: 0.1 Author URI: http://stephanieleary.com/ */ function scl_get_plugin_count() { $all_plugins = apply_filters( 'all_plugins', get_plugins() ); foreach ( (array) $all_plugins as $plugin_file => $plugin_data) { if ( is_plugin_active($plugin_file) ) { $active_plugins[ $plugin_file ] = $plugin_data; } } $total_active_plugins = count($active_plugins); echo '<p class="plugins-right-now">You have <a href="plugins.php">'.$total_active_plugins.' active plugins</a>.'; } add_action('activity_box_end', 'scl_get_plugin_count'); ?>
Save this as a PHP file in your wp-content/plugins
directory (I just called it count-plugins.php
) and activate it.
Of course, if you have a lot of plugins, I suppose this might slow down your Dashboard a little.
ETA: There’s a much shorter solution in the comments!
Ozh says
You’re looping over lots of useless data here. You just need to echo count( get_option( ‘active_plugins’ ) )
steph says
Well, I thought so too at first, but why on earth doesn’t plugins.php do it that way?
Ozh says
Because plugins.php has to retrieve a number of info for each plugins: title, urls, version, is it active, etc…
steph says
Ah yes, this is much shorter if all you need is the count!
Andrew Nacin says
I was coming here to suggest the same thing as Ozh. get_plugins() reads the entire directory of plugins, verifies files and plugin headers, and the like. It’s slow, and you probably don’t want to slow your dashboard down that much. Calling get_option(‘active_plugins’) is a cheap and easy way for the same result.
Ron says
In addition to what @Ohz said, if you want to make it WP network friendly
$count = count( get_option( ‘active_plugins’ ) );
if( is_multisite() )
$count += count( get_site_option( ‘active_sitewide_plugins’, array() ) );
echo $count;
steph says
Cool. Thanks, Ron!