解决Envato Theme Check报错:用function_exists()/class_exists()替代is_plugin_active()
is_plugin_active() with function_exists() or class_exists() for Envato Theme Check Compliance Got it, let's work through fixing this Envato Theme Check warning together. The problem with is_plugin_active() is that it depends on a WordPress admin file (wp-admin/includes/plugin.php) that might not always be loaded in every context, which is why Envato flags it as unreliable. We can swap it out for a more robust check using either function_exists() or class_exists()—here's how:
First, let's recap the warning you received:
REQUIRED: Found is_plugin_active( in the file plugin-activation.php. is_plugin_active() is not reliable. Use function_exists() or class_exists() instead.
Your original code snippet looks like this:
if ( $this->is_automatic && ! $this->is_plugin_active( $slug ) ) { $plugin_activate = $upgrader->plugin_info(); if ( false === $this->activate_single_plugin( $plugin_activate, $slug, true ) ) { return true; } }
Option 1: Use function_exists() (Best if the plugin has a unique public function)
Most plugins register a unique function when they activate—something that only exists when the plugin is running. For example, WooCommerce uses woocommerce_init(), and Yoast SEO uses wpseo_init().
Find that unique function for the plugin you're checking, then replace your condition with a check for that function's existence:
// Replace 'your_target_plugin_unique_function' with the actual function from the plugin if ( $this->is_automatic && ! function_exists( 'your_target_plugin_unique_function' ) ) { $plugin_activate = $upgrader->plugin_info(); if ( false === $this->activate_single_plugin( $plugin_activate, $slug, true ) ) { return true; } }
Option 2: Use class_exists() (Best if the plugin uses a core class)
If the plugin is built around a main class (which many modern plugins are), you can check for that class instead. Again, use a class that's only loaded when the plugin is active—like WooCommerce for WooCommerce or WPSEO_Options for Yoast SEO.
Here's how to adjust your code:
// Replace 'Your_Target_Plugin_Core_Class' with the actual class from the plugin if ( $this->is_automatic && ! class_exists( 'Your_Target_Plugin_Core_Class' ) ) { $plugin_activate = $upgrader->plugin_info(); if ( false === $this->activate_single_plugin( $plugin_activate, $slug, true ) ) { return true; } }
Option 3: Check the Active Plugins List (If you need to stick with the slug)
If you still want to use the plugin slug for your check (instead of a function/class), you can directly query WordPress's active_plugins option. This avoids any dependency on the is_plugin_active() function:
// Get the list of active plugins from the database $active_plugins = get_option( 'active_plugins', array() ); // Build the full plugin file path (e.g., "plugin-slug/plugin-slug.php"—adjust if the main file has a different name) $plugin_main_file = $slug . '/' . $slug . '.php'; // Update your condition to check against the active plugins list if ( $this->is_automatic && ! in_array( $plugin_main_file, $active_plugins ) ) { $plugin_activate = $upgrader->plugin_info(); if ( false === $this->activate_single_plugin( $plugin_activate, $slug, true ) ) { return true; } }
Why these fixes work:
function_exists()andclass_exists()check directly for code that's loaded when the plugin is active—no reliance on admin-only files, making them reliable in any context.- Querying the
active_pluginsoption pulls directly from WordPress's database, which is the source of truth for which plugins are marked as active.
内容的提问来源于stack exchange,提问作者saagar

