Hi @haet,
Thanks for the is_plugin_active(), really appreciate it. To be honest, I didn’t know about this function. Will have to change it in all my plugins now :/ Not sure what could cause the compatibility issue with the old code though…
Hi again, @haet,
I’ve tried it out and now I remembered that I actually already tried using is_plugin_active() – it produces Call to undefined function is_plugin_active() error on my server. The problem that this function is only defined in wp-admin/includes/plugin.php file, which loads later. So basically is_plugin_active() should be used in admin_init hook for example. In your case, I assume, some other plugin is requiring wp-admin/includes/plugin.php file earlier, which I’m not sure is a good idea. Anyway, I think I found a solution that should work on all servers – it will be added in next plugin version.
You could / should wrap your initialization code in
add_action( 'plugins_loaded', function(){
// your init code here
});
so you can make sure the WordPress core is initialized.
@haet,
Yes, I’ve also thought of plugins_loaded hook.
Thanks for the fast reply!
Hi @haet,
I’ve just tried it with plugins_loaded, but it looks like is_plugin_active() is still not defined yet. The only hook that I found which has this function already defined is admin_init. Also it looks like it’s available in admin only – I’ve tried it in init hook and it’s not defined there.
So I’m thinking about defining my own function:
/*
* alg_is_plugin_active.
*/
function alg_is_plugin_active( $plugin ) {
return ( function_exists( 'is_plugin_active' ) ? is_plugin_active( $plugin ) :
(
in_array( $plugin, apply_filters( 'active_plugins', (array) get_option( 'active_plugins', array() ) ) ) ||
( is_multisite() && array_key_exists( $plugin, (array) get_site_option( 'active_sitewide_plugins', array() ) ) )
)
);
}
So basically I’m using is_plugin_active() if it’s already defined (like in your case) and otherwise I’m using my own code (which is basically same code copied from is_plugin_active() source). This should solve the issue on your server. Still not sure why your server doesn’t like my old code though – it’s basically the same code as in original WP.
P.S. The only difference I’m seeing in the original WP code and my code is that I’m applying active_plugins filter on get_option( 'active_plugins', array() ). I’ve copied this filter from WooCommerce – in class-wc-helper.php file:
$active_plugins = apply_filters( 'active_plugins', get_option( 'active_plugins' ) );
P.P.S. I’ve just released new plugin v1.4.1 with the updated code as described above.
FYI, it would be better practice to use:
// Include file if function does not exist
if (!function_exists('is_plugin_active')) {
include_once(ABSPATH . 'wp-admin/includes/plugin.php');
}
Other than making your own. Sure it works but be careful with copying from core, especially if they change how that works. Minor issue but worth mentioning.