Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

share|improve this question
    
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
add comment

5 Answers

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.

share|improve this answer
add comment

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' );
share|improve this answer
    
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
add comment

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 (http://codex.wordpress.org/File_Header)? Is your plugin being included after the above plugin.php file is loaded?

share|improve this answer
    
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
add comment

This isn't a great answer (but I don't have rep to comment on someone else's answer yet), but a quick n 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.

share|improve this answer
add comment

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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