WordPress GuideDevelopment → Activate Plugin

WordPress: Activate plugin if theme is active (a how-to)

Image

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:

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.

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.

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:

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:

Replace:

Important caveats and tips

This method is simple, but keep these things in mind:

Ready to get started?

Get the fastest, most secure WordPress.org hosting on the market.

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!

Trust us to help you choose the ideal hosting solution

Loading form…