Skip to content

Instantly share code, notes, and snippets.

@dlxsnippets
Created September 12, 2022 05:05
Show Gist options
  • Save dlxsnippets/e51308d920282989a866915b20bb5460 to your computer and use it in GitHub Desktop.
Save dlxsnippets/e51308d920282989a866915b20bb5460 to your computer and use it in GitHub Desktop.
Check if a plugin is active for a site
<?php
/**
* Checks to see if an asset is activated or not.
*
* @since 1.0.0
*
* @param string $path Path to the asset.
* @param string $type Type to check if it is activated or not.
*
* @return bool true if activated, false if not.
*/
public static function is_activated( $path, $type = 'plugin' ) {
// Gets all active plugins on the current site.
$active_plugins = self::is_multisite() ? get_site_option( 'active_sitewide_plugins' ) : get_option( 'active_plugins', array() );
if ( in_array( $path, $active_plugins, true ) ) {
return true;
}
return false;
}
// Needs helper function below.
/**
* Checks if the plugin is on a multisite install.
*
* @since 1.0.0
*
* @param bool $network_admin Check if in network admin.
* @param string $slug Plugin slug.
*
* @return true if multisite, false if not.
*/
public static function is_multisite( $network_admin = false, $slug = 'alerts-dlx' ) {
if ( ! function_exists( 'is_plugin_active_for_network' ) ) {
require_once ABSPATH . '/wp-admin/includes/plugin.php';
}
$is_network_admin = false;
if ( $network_admin ) {
if ( is_network_admin() ) {
if ( is_multisite() && is_plugin_active_for_network( $slug ) ) {
return true;
}
} else {
return false;
}
}
if ( is_multisite() && is_plugin_active_for_network( $slug ) ) {
return true;
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment