butlerblog
10/5/2018 - 12:54 PM

Utility to get plugin info

Utility to get plugin info

<?php // no need to use this line.

/**
 * Utility to get information on installed plugins.
 *
 * Returns an array of all installed plugins and indicates which are
 * plugin are active and which are not. Array is keyed by the plugin's
 * folder/slug.php (which is how WP looks at them) and includes the
 * name, version, and true/false whether it is active or not.
 *
 * @see: https://codex.wordpress.org/Function_Reference/get_plugins
 * @see: https://developer.wordpress.org/reference/functions/get_plugins/
 * @see: https://developer.wordpress.org/reference/functions/get_option/
 *
 * @return $plugins array {
 *     An array of all installed plugins.
 *
 *     @type array $plugin {
 *         The plugin information (note: array key is folder/slug.php)
 *
 *         @type string  $name
 *         @type string  $version
 *         @type boolean $active
 *     }
 * }
 */
function my_get_plugin_info() {

	// Get all plugins
	include_once( 'wp-admin/includes/plugin.php' );
	$all_plugins = get_plugins();

	// Get active plugins
	$active_plugins = get_option( 'active_plugins' );
	
	// Assemble array of name, version, and whether plugin is active (boolean)
	foreach ( $all_plugins as $key => $value ) {
		$is_active = ( in_array( $key, $active_plugins ) ) ? true : false;
		$plugins[ $key ] = array(
			'name'    => $value['Name'],
			'version' => $value['Version'],
			'active'  => $is_active,
		);
	}
	
	return $plugins;
}
<?php // No need to use this line.

/**
 * Utility to get information on installed plugins.
 *
 * This utility gets the full installed plugin list (get_plugins()) and the
 * active plugin list (get_option( 'active_plugins' )) and adds the data
 * from the active plugin list to the full list as a boolean (key: 'Active').
 *
 * @see: https://codex.wordpress.org/Function_Reference/get_plugins
 * @see: https://developer.wordpress.org/reference/functions/get_plugins/
 * @see: https://developer.wordpress.org/reference/functions/get_option/
 *
 * @return $plugins array {
 *     An array of all installed plugins.
 *
 *     @type array $plugin {
 *         The plugin information (note: array key is folder/slug.php)
 *
 *         @type string  $Name
 *         @type string  $PluginURI
 *         @type string  $Version
 *         @type string  $Description
 *         @type string  $Author
 *         @type string  $AuthorURI
 *         @type string  $TextDomain
 *         @type string  $DomainPath
 *         @type boolean $Network
 *         @type string  $Title
 *         @type string  $AuthorName
 *         @type boolean $Active
 *     }
 * }
 */
function my_get_plugins() {
	
	// Get all plugins
	include_once( 'wp-admin/includes/plugin.php' );
	$all_plugins = get_plugins();

	// Get active plugins
	$active_plugins = get_option( 'active_plugins' );
	
	// Add 'Active' boolean to $all_plugins array.
	foreach ( $all_plugins as $key => $value ) {
		$is_active = ( in_array( $key, $active_plugins ) ) ? true : false;
		$all_plugins[ $key ]['Active'] = $is_active;
	}
	
	return $all_plugins;	
}