is_plugin_active function doesn’t exist

The question:

I’m using WordPress 3.0.5 and have tested with 3.1rc4. In the main PHP file of my plugin, when I try to call is_plugin_active I get Call to undefined function is_plugin_active(). I can call add_action and add_filter. What should I check/change to fix this?

This is happening inside of the admin on the Plugins page. At the top of my main plugin file I have, if (function_exists('is_plugin_active')) { which always returns false.

I also can’t see the functions from my main plugin file in other plugins (if that helps any).

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

That’s because the file in which is_plugin_active() is defined – wp-admin/includes/plugin.php – is only loaded in the admin, after your plugin is loaded.

Thus, you can only call it after ‘admin_init’ has fired:

function check_some_other_plugin() {
  if ( is_plugin_active('some-plugin.php') ) {
    ...
  }
}
add_action( 'admin_init', 'check_some_other_plugin' );

Method 2

You can call is_plugin_active() in templates and from user pages as well, which can be useful for related plugins (i.e. require plugin ‘xyz’ to operate my new plugin). You need to manually include the plugin.php file as noted in the is_plugin_active() docs on Codex.

Here is a functional example I use in my premium add-on packs to make sure the free base plugin is active before invoking a the add-on object. It requires certain hooks & filters to be available in the base plugin or it will crash.

include_once(ABSPATH.'wp-admin/includes/plugin.php');
if (!function_exists('is_plugin_active') || !is_plugin_active('store-locator-le/store-locator-le.php')) { 
   return;
}

As an aside, if you are having problems with is_plugin_active() from within an active admin login it is likely because you are testing before admin_init fires. admin_menu fires before admin_init and doing tests in admin_menu has “bitten” me before. The name “admin_init” which seems counter-intuitive to me since admin_menu is already run. I think of init as “first thing to run”… maybe admin_kinda_init() would be better. 🙂

Method 3

A quick and dirty workaround would be to duplicate the function manually:

if ( ! function_exists( 'is_plugin_active' ) ) {
    function is_plugin_active( $plugin ) {
        return in_array( $plugin, (array) get_option( 'active_plugins', array() ) );
    }
}

It’s pretty short so it shouldn’t be too hard to implement in your own code as a workaround.

Method 4

I preferred Tom Auger’s answer with how to define your own is_plugin_active function instead of including plugin.php. However, using the same function name caused my admin area to crash. Using a different function name solved it:

function is_plugin_active_byme( $plugin ) {
    return in_array( $plugin, (array) get_option( 'active_plugins', array() ) );
}

Method 5

Simple and easy. Taken from WP

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

and then any instances of

 if ( is_plugin_active( 'feed-them-social/feed-them.php' ) ) {

   }

get the function check. SO no more errors.

Method 6

Are you using is_plugin_active inside the admin or the front-end? I am pretty sure that is_plugin_active is only an admin function that is found inside wp-admin/includes/plugin.php.

Additionally, does your main plugin file make use of WordPress File Header? Is your plugin being included after the above plugin.php file is loaded?

Method 7

To debug active plugins:

var_dump(get_option(...));

To check for a specific plugin:

in_array(get_option(...));

The action is called “active_plugins” IIRC.

Method 8

If you’re using elementor and facing the issue only in the frontend and it’s working in the editor view.

You can use the is_edit_mode() function of elementor, so the condition will only run in the editor view.

if ( ElementorPlugin::$instance->editor->is_edit_mode() && is_plugin_active('some-plugin.php') ) {
    return true;
} else {
    return false;
}

Method 9

You can check if a plugin is active as follows:

/**
 * Check if Some Plugin is active
 **/
if ( in_array( 'some-plugin/some-plugin.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
    // Put your plugin code here
}

Method 10

None of these solutions worked for me, but this did:

include_once('wp-admin/includes/plugin.php');

I found this solution at codebangers.com


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment