7

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.

  • 1
    I would STRONGLY recommend against including a plugin file from inside a theme like that. That function is also only available inside the admin area – Tom J Nowell Apr 25 '15 at 1:51
15

Try to check class_exists:

<?php 
     if( class_exists('acf') ) {
        echo "hi";
     }
?>
  • This was the solution to my problem. Check updated question. – Johann Apr 25 '15 at 19:03
6

is_plugin_active() only available inside the admin area. You need to include the core plugin.php file in front end to use this function.

From WordPress documentation

NOTE: defined in wp-admin/includes/plugin.php, so this is only available from within the admin pages, and any references to this function must be hooked to admin_init or a later action. If you want to use this function from within a template, you will need to manually require plugin.php, an example is below.

So it will be like

<?php include_once( ABSPATH . 'wp-admin/includes/plugin.php' ); 
if ( is_plugin_active( 'advanced-custom-fields-pro/acf.php' ) ) {
    echo "hi";
} ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.