9

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
  • 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, 2015 at 1:51

2 Answers 2

17

Try to check class_exists:

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

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 and acknowledge that you have read and understand our privacy policy and code of conduct.

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