Skip to content

Instantly share code, notes, and snippets.

@josephdickson
Last active August 12, 2019 22:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save josephdickson/0c5ef71f5642a0cb3de31f1ec5d31976 to your computer and use it in GitHub Desktop.
Save josephdickson/0c5ef71f5642a0cb3de31f1ec5d31976 to your computer and use it in GitHub Desktop.
WordPress Dashboard Widgets display active plugins on a site
<?php
/**
* @package jd-check-plugin-activity
* @version 1.0
*/
/*
Plugin Name: Check Plugin Activity
Plugin URI: https://joseph-dickson.com
Description: Dashboard widgets that display which plugins are active.
Author: Joseph Dickson
Version: 1.0
Author URI: https://joseph-dickson.com
*/
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
/**
* Adapted from https://wordpress.stackexchange.com/questions/54742/how-to-do-i-get-a-list-of-active-plugins-on-my-wordpress-blog-programmatically
*/
/**
* ## Display Dashbord Widgets
*
* Display Single Site Dashboard Widget
*/
add_action('wp_dashboard_setup', 'jd_dashboard_setup');
function jd_dashboard_setup() {
// Site Active Plugins
wp_add_dashboard_widget( 'jd_active_site_plugins', __( 'Active Plugins' ), 'jd_active_site_plugins' );
// Network Active Plugins
if( is_multisite() ) {
wp_add_dashboard_widget( 'jd_active_network_plugins', __( 'Network Active Plugins' ), 'jd_active_network_plugins' );
}
}
/**
* Display Multisite Dashboard Widget
*/
add_action('wp_network_dashboard_setup', 'jd_network_dashboard_setup');
function jd_network_dashboard_setup() {
// Network Active Plugins
wp_add_dashboard_widget( 'jd_active_network_plugins', __( 'Network Active Plugins' ), 'jd_active_network_plugins' );
// Sites on this Network
wp_add_dashboard_widget( 'jd_single_site_plugins', __( 'Plugins Running on the Network' ), 'jd_single_site_plugins' );
}
/**
* ## Get Activated Plugin Information
*
* Locally Activated Plugins
*/
function jd_active_site_plugins() {
$the_plugins = get_option('active_plugins');
echo '<h1>Activated Plugins</h1>';
echo '<ul>';
asort($the_plugins); // sort $the_plugins array and list results alphabetically
foreach($the_plugins as $key => $value) {
$string = explode('/',$value); // plugin slug will be displayed
echo '<li>'.$string[0] .'</li>';
}
echo '</ul>';
}
/**
* Network Activated Plugins
*/
if( is_multisite() ) {
function jd_active_network_plugins() {
$the_plugins = get_site_option('active_sitewide_plugins');
echo '<p>Network activated plugins running on this site.</p>';
echo '<ul>';
asort($the_plugins); // sort $the_plugins array and list results alphabetically
foreach($the_plugins as $key => $value) {
$string = explode('/',$key); // plugin slug will be displayed
echo '<li>'.$string[0] .'</li>';
}
echo '</ul>';
}
}
/**
* Plugins Activated Locally on Each Site
*/
function jd_single_site_plugins() {
global $wpdb;
$blogs = $wpdb->get_results("
SELECT blog_id
FROM {$wpdb->blogs}
WHERE site_id = '{$wpdb->siteid}'
AND spam = '0'
AND deleted = '0'
AND archived = '0'
");
echo '<p>Plugins activavted locally on each site.</p>';
foreach ($blogs as $blog) {
$the_plugins = get_blog_option( $blog->blog_id, 'active_plugins');
echo '<h1>'. get_blog_option($blog->blog_id, 'blogname') . '</h1>';
echo '<ul>';
asort($the_plugins); // sort array and list results alphabetically
foreach($the_plugins as $key => $value) {
$string = explode('/',$value); // plugin slug will be displayed
echo '<li>'.$string[0] .'</li>';
}
echo '<hr />';
echo '</ul>';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment