◦ Comprehensive security
◦ 24/7 support
WordPress Guide → Development → Activate Plugin
WordPress: Activate plugin if theme is active (a how-to)
Need a WordPress plugin to activate automatically when a certain theme is in use? You can do that with just a few lines of PHP. Let’s walk through how to set it up the right way.
Let’s walk through your best options step-by-step, whether you’re using a plugin or syncing files manually.
Get fast, reliable hosting for WordPress
Power your site with the industry’s fastest, most optimized WordPress hosting
Why activate a plugin based on the active theme?
This setup is useful when your theme and plugin are tightly linked and need to work together. For example:
- You’ve built a custom plugin that adds key features or layout components to a custom theme.
- Your theme depends on specific plugin functionality, like a page builder, shortcode library, or slider plugin.
- You’re packaging a theme and plugin as a client deliverable or premium product.
Activating the plugin automatically removes one more manual step from the setup process and ensures the site runs as intended the moment the theme is activated.
1. Prepare your theme’s functions.php file
The easiest way to add this behavior is by editing your theme’s functions.php file.
Option A: Use your child theme’s functions.php
If you’re already working in a child theme, open or create the functions.php file in your child theme directory (usually at /wp-content/themes/your-child-theme/). This file loads with every page load and is the proper place to add theme-based functionality.
Option B: Use a custom plugin
If you’d rather keep logic out of the theme, consider creating a small custom plugin to handle this. But for this guide, we’ll focus on functions.php since the behavior is tightly coupled with the theme being active.
2. Hook into after_setup_theme
WordPress runs the after_setup_theme action after the theme has been initialized. This is the ideal point to check the theme status and run your plugin activation logic.
Here’s how you hook into it:
add_action( ‘after_setup_theme’, ‘activate_plugin_with_theme’ );
This tells WordPress to run your custom function after the theme setup is complete.
3. Check the active theme using is_active_theme()
Inside the function, use is_active_theme() to check if the correct theme is active. This ensures your logic only runs when the expected theme is being used.
function activate_plugin_with_theme() { if ( is_active_theme( 'your-theme-name' ) ) { activate_plugin( 'plugin-folder/plugin-file.php' ); } }Replace ‘your-theme-name’ with the directory name of your theme (not the display name). You can find this by looking at your theme folder under /wp-content/themes/.
4. Use activate_plugin() to enable the plugin
The activate_plugin() function tells WordPress to activate a specific plugin. It takes one argument: the relative path to the plugin file inside the wp-content/plugins/ directory.
activate_plugin( 'plugin-folder/plugin-file.php' );Make sure the plugin path is accurate. If your plugin is in a folder named custom-plugin and the main file is custom-plugin.php, the full path would be:
activate_plugin( 'custom-plugin/custom-plugin.php' );Full example: auto-activate plugin if theme is active
Here’s the complete code sample you can drop into your theme’s functions.php file:
function activate_plugin_with_theme() { if ( is_active_theme( 'your-theme-name' ) ) { activate_plugin( 'plugin-folder/plugin-file.php' ); } } add_action( 'after_setup_theme', 'activate_plugin_with_theme' );Replace:
- ‘your-theme-name’ with the folder name of your theme.
- ‘plugin-folder/plugin-file.php’ with the correct plugin path relative to the wp-content/plugins/ directory.
Important caveats and tips
This method is simple, but keep these things in mind:
- The plugin must already be installed. This method won’t install the plugin—only activate it.
- It triggers only on theme load. The activation happens once when the theme is loaded. If a user later deactivates the plugin, it won’t be re-activated unless the theme is switched away and then re-enabled again.
- Use with caution in distributed themes. If you’re building a product for distribution, make sure the plugin path is standardized or the plugin is bundled properly. Consider using TGM Plugin Activation for more robust handling.
Next steps for activating plugins based on the theme
Pairing themes with plugins like this helps deliver a seamless user experience and reduces manual setup. It’s especially useful for developers working on client projects, site bundles, or commercial themes.
If you want to go further, you can also:
- Show admin notices when required plugins are missing.
- Add checks to ensure the plugin isn’t already active.
- Trigger plugin deactivation when the theme is switched (using the switch_theme action).
Ready to upgrade your WordPress experience? Professional hosting improves speeds, security, and reliability for a website and a brand that people find engaging and trustworthy.
Don’t want to deal with server management and maintenance? Our fully managed hosting for WordPress is the best in the industry. Our team are not only server IT experts, but WordPress hosting experts as well. Your server couldn’t be in better hands.
Click through below to explore all of our hosting for WordPress options, or chat with a WordPress expert right now to get answers and advice.
Additional resources
What is managed WordPress hosting? →
Get details and decide if managed WordPress hosting is right for you.
How to push a (WordPress) staging site without overwriting the database →
Learn how to push a WordPress staging site to live without overwriting the existing database.
A complete guide to WordPress shortcodes →
Shortcodes make life easier. Learn how to get started!