Unable to check if plugin is active

I am working on a custom theme using ACF. I would like to check if the plugin is active or not. I am using this code:

<?php include_once( ABSPATH . 'wp-content/plugins/advanced-custom-fields-pro/acf.php' ); 
if ( is_plugin_active( 'advanced-custom-fields-pro/acf.php' ) ) {

echo "hi";

} ?>

However, I am getting the following error:

Fatal error: Call to undefined function is_plugin_active() in /Users/johann/htdocs/clarity_v21/wp-content/themes/clarity/templates/header.php on line 21

Any ideas what could be wrong?


So based on the provided answer I tried:

if( class_exists('acf') ) { 

  if (($header_style)=='style2') {

}

and it worked! So basically the solution is to try to find a class that’s related to the plugin you are trying to check on. In this case, the class “acf” is specific to the Advanced Custom Fields plugin and allowed me to run the conditional only if the plugin was active.

2 s
2

Try to check class_exists:

<?php 
     if( class_exists('acf') ) {
        echo "hi";
     }
?>

Leave a Comment