Plugin aktiv/inaktiv?

Bei der Programmierung von Themes oder Plugins ist es manchmal relevant, ob ein Plugin aktiv oder inaktiv ist. Danach richtet sich das Verhalten von Themes oder anderen Plugins.

Grundsätzlich muss zuerst geprüft werden, ob die Funktion existiert.

Beispiel für die Prüfung, ob ein Plugin aktiv ist:

if (!function_exists('is_plugin_active')) {
require_once ABSPATH . '/wp-admin/includes/plugin.php';
}

 

Beispiel für die Prüfung, ob ein Plugin inaktiv ist:

if (!function_exists('is_plugin_inactive')) {
require_once ABSPATH . '/wp-admin/includes/plugin.php';
}

 

Danach schließt sich die eigentliche Funktion an (Beispiel für das Plugin “HappyForms”):

if (!function_exists('is_plugin_active')) {
require_once ABSPATH . '/wp-admin/includes/plugin.php';
}
if (is_plugin_active('happyforms/happyforms.php')) {
// Aktion ausführen
}

 

Beispiel für ein aktives Plugin mit Benachrichtigung durch die Admin-Notice:

<?php
/*
Stand: 25.01.2024
Check, ob Plugins aktiv/inaktiv sind
*/
// Plugin "WP H-Happyforms Tools" aktiv?
if (!function_exists('is_plugin_active')) {
require_once ABSPATH . '/wp-admin/includes/plugin.php';
}
if (is_plugin_active('wp-h-happyforms-tools/wphhft.php')) {
function wphhft_notice() { ?>
<div class="notice notice-error">
<p><?php _e('Bitte das Plugin <b>"WP H-Happyforms Tools"</b></a> deaktivieren und l&ouml;schen!');?></p>

</div>
<?php
}
add_action( 'load-index.php',
function(){
add_action( 'admin_notices', 'wphhft_notice' );
}
);
}
?>

Hinweis:
Wenn das gesuchte Plugin aktiv ist, erfolgt eine Meldung durch die Admin-Notice.