Title: Activate a plugin through code
Author: WordPress VIP Documentation
Published: March 10, 2021
Last modified: December 31, 2025

---

 1. [Plugins](https://docs.wpvip.com/plugins/)
 2. Activate a plugin through code

#  Activate a plugin through code

Plugins must be installed by adding them to the [`/plugins` directory](https://docs.wpvip.com/wordpress-skeleton/plugins-directory/)
of [an application’s GitHub repository](https://docs.wpvip.com/github-repository/)
in order to be available for activation in the WordPress Admin dashboard (WP Admin).
As an alternative to manual activation, it is recommended to code-activate plugins
instead to gain greater control and consistency across all environments (e.g., production,
non-production, and local development environments).

By default a [`plugin-loader.php`](https://docs.wpvip.com/wordpress-skeleton/client-mu-plugins-directory/)
file is located within [the `/client-mu-plugins` directory](https://docs.wpvip.com/wordpress-skeleton/client-mu-plugins-directory/).
Use this file to selectively code-activate third-party plugins that are located 
in `/plugins`, and custom plugins that are located in subdirectories of `/client-
mu-plugins`.

## Determine a plugin’s name for code activation

A plugin’s “name” will most often be identical to the directory encapsulating the
plugin. For example, a plugin directory named `my-plugin` will most often have a
main file directly within it named `my-plugin.php`. Because the directory and the
main file naming match, the plugin’s name is: `my-plugin`.

When a plugin’s main file is named _differently_ than the directory, the name may
include the path to the plugin’s main file. For example, a plugin directory named`
my-plugin` that has a main file directly within it named `plugin.php`. Because the
directory and the main file naming do not match, the plugin’s name is: `my-plugin/
plugin.php`.

If a plugin consists of only a single PHP file, and it is installed to the root 
of `/plugins` (e.g. `/plugins/my-plugin.php`), the plugin name may be the file name,
for example: `my-plugin.php`.

Output from [the WP-CLI command `wp plugin list`](https://developer.wordpress.org/cli/commands/plugin/)
can be used to retrieve a plugin’s name. The `&plugin=` parameter value visible 
in the link to activate (or deactivate) a plugin on a site’s WP Admin Plugins dashboard
will also include a plugin’s name.

## Code-activate a plugin in `/plugins`

[Third-party plugins](https://docs.wpvip.com/plugins/third-party-plugins/) should
be installed by adding them to an application’s [`/plugins`](https://docs.wpvip.com/wordpress-skeleton/plugins-directory/)_
[ ](https://docs.wpvip.com/wordpress-skeleton/plugins-directory/)_[directory](https://docs.wpvip.com/wordpress-skeleton/plugins-directory/).
The plugins can be code-activated with the `[wpcom_vip_load_plugin()](https://github.com/Automattic/vip-go-mu-plugins/blob/09de0f928530d6c5dbd8f56e4f5d50253798d7f4/vip-helpers/vip-utils.php#L1088)`
function.

The `wpcom_vip_load_plugin()` function _only_ works for plugins that have been installed
in the `/plugins` directory of an application’s GitHub repository.

If the plugin name cannot be resolved to a plugin in the `/plugins` directory, the`
wpcom_vip_load_plugin()` function will throw an `E_USER_WARNING` type error.

Activate a plugin by passing the plugin’s name in the `wpcom_vip_load_plugin()` 
function in `/client-mu-plugins/plugin-loader.php`.

/client-mu-plugins/plugin-loader.php

    ```lang-php
    // Activate the plugin located in `/plugins/plugin-name`.
    wpcom_vip_load_plugin( 'plugin-name' );

    // Activate a specific plugin file: `/plugins/other-plugin-name/plugin-name.php`.
    wpcom_vip_load_plugin( 'other-plugin-name/plugin-name.php' );
    ```

When a plugin is successfully activated through code, the text “**Enabled via code**”
will be displayed under the plugin’s name in the list of plugins on the site’s WordPress
Admin dashboard. The action links that usually allow users to Activate or Deactivate
the plugin through the admin user interface will be removed and unavailable.

## Code-activate a plugin in `/client-mu-plugins`

Plugins that are installed in `/client-mu-plugins`, and are programmatically activated,
are automatically included early in the [WordPress load order](https://docs.wpvip.com/plugins/load-order/)
as [MU (“must use”) plugins](https://wordpress.org/support/article/must-use-plugins/).
Only custom plugins with code that needs to be auto-loaded or run earlier in the
WordPress load process should be installed in `/client-mu-plugins`. 

**Note**

Plugins that are located in the root of `/client-mu-plugins` and consist entirely
of a single PHP file (e.g., `/client-mu-plugins/plugin-name.php`) will be activated
automatically and should not be added to the plugin loader file. If code is added
to `plugin-loader.php` to activate a single-file plugin, that plugin will load twice
and will likely cause issues.

To avoid loading a plugin twice, only plugins contained within their own subdirectory(
e.g., `client-mu-plugins/my-plugin/my-plugin.php`) should be activated via [`/client-mu-plugins/plugin-loader.php`](https://github.com/Automattic/vip-go-skeleton/blob/master/client-mu-plugins/plugin-loader.php).

Use the `WPCOM_VIP_CLIENT_MU_PLUGIN_DIR` constant to point to an application’s `/
client-mu-plugins`, followed by the path to the plugin’s main file.

In this code example, a plugin named `/plugin-name/plugin-name.php` located in `/
client-mu-plugins` is activated via code and loaded as an MU plugin:

/client-mu-plugins/plugin-loader.php

    ```lang-php
    <?php
    require WPCOM_VIP_CLIENT_MU_PLUGIN_DIR . '/plugin-name/plugin-name.php';
    ```

## Code-activate a plugin on a multisite

Plugins loaded with `wpcom_vip_load_plugin()` on a [WordPress multisite](https://docs.wpvip.com/wordpress-multisite/)
will be network-activated, unless logic is added to selectively activate that plugin
on a per-site basis by using the [`get_current_blog_id()` function](https://developer.wordpress.org/reference/functions/get_current_blog_id/).

In this code example, a plugin located in a directory named `plugin-name` will be
activated only for network site ID 2, while a plugin located in a directory named`
common-plugin` will be activated for all sites on the network:

    ```lang-php
    // Activate the plugin located in `/plugins/plugin-name/` for network site ID 2.
    if ( get_current_blog_id() === 2 ) {
        wpcom_vip_load_plugin( 'plugin-name' );
    }
    // Activate the plugin located in `/plugins/common-plugin/`for all sites on the network.
    wpcom_vip_load_plugin( 'common-plugin' );
    ```

## Code-activate a plugin in theme or plugin code

Activating plugins through code from within a theme or plugin is not recommended.

Whenever possible, use the `/client-mu-plugins` directory methods described above
to call the `wpcom_vip_load_plugin()` function before the [`plugins_loaded` hook](https://developer.wordpress.org/reference/hooks/plugins_loaded/)
is fired.

If a plugin’s functionality absolutely must be tied to a theme, the `wpcom_vip_load_plugin()`
function should be called before the [`after_setup_theme` hook](https://developer.wordpress.org/reference/hooks/after_setup_theme/).
Though this method is supported, doing so will trigger a “called incorrectly” PHP
notice.

## Plugins that require an activation hook

Some plugin code expects that the plugin will be manually activated in the WordPress
Admin dashboard. That plugin code often relies on [`register_activation_hook()`](https://developer.wordpress.org/reference/functions/register_activation_hook/)
to fire and run functions which establish conditions which the plugin relies on 
to work as expected.

The `register_activation_hook()` will not fire if a plugin is code-activated. For
plugins that rely on `register_activation_hook()`, a couple of extra steps can ensure
that the new plugin gets set up correctly:

 1. [Install the plugin](https://docs.wpvip.com/plugins/installing-plugins/) by committing
    it to the application’s GitHub repository.
 2. Activate the plugin from the site’s WordPress Admin dashboard [Plugins screen](https://wordpress.org/support/article/plugins-screen/).
 3. Follow instructions above to code-activate the plugin in `plugin-loader.php`_._

Last updated: December 31, 2025