15

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).

  • I am not sure from your description - if this is issue with activated plugin or if you are trying to do something on activation or under other specific conditions? – Rarst Feb 15 '11 at 19:15
  • I've found that function_exists('is_plugin_active') is false when called from the main plugin file if outside of a method but is true when called inside of the method that is called for the activated_plugin action. – Carl Feb 16 '11 at 3:36
  • On which page do you see the error? The plugin's page (wp-admin/plugins.php)? Is this before or after you activate the plugin in question? Rarst has a good point. Do you know at what point you are calling the function with in wordpress? – Jonnybojangles Feb 16 '11 at 19:58
  • I see the error on wp-admin/plugins.php after I activate the plugin. I have the check in some of the methods that are hooked in as actions and it works there. Seems like it has something to do with inclusion order. – Carl Feb 16 '11 at 23:37
  • Plugins are loaded in alphabetical order, so it may simply be that your plugin isn't being processed early enough. Try renaming your plugin to start with the letter a (eg a_myplugin-name) so that it loads earlier. The best option however would be to call your function_exists block from within a function that runs after plugins are loaded. You can see the rough running order on the WP codex: codex.wordpress.org/Plugin_API/Action_Reference – Phill Healey Feb 19 at 19:07
26

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' );
  • If I'm already in the admin, is there something that might be configured wrong that would disallow me access to this? – Carl Feb 15 '11 at 14:03
  • 1
    See updated answer. – scribu Apr 30 '11 at 0:06
10

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. :)

  • Adding include_once(ABSPATH.'wp-admin/includes/plugin.php'); did the trick for me. Thanks! – Dave Apr 12 '18 at 0:42
8

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

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.

4

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.

  • 1
    If you're using requireonce you probably don't need to check the function... ? – Phill Healey Mar 23 '17 at 20:07
  • You do that so you can make sure the plugin is defined before trying to use it, otherwise you could run into troubles down the line. – Spencer Labadie Feb 19 at 18:06
3

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() ) );
}
  • I agree. While you can check for conflict in your declaration of the function, WP does not and hence your WP will trigger an error. – Chris Sprague Sep 9 '18 at 10:54
1

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?

  • This is happening inside of the admin on the Plugins page. The plugin does have the suggested WP file header. How can I check if the plugin is being included after plugin.php? – Carl Feb 15 '11 at 14:02
1

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.

0

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

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

I found this solution at codebangers.com

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.