on Writings, Tutorials, CMS, WordPress, PHP

How to secretly activate WordPress plugins

Need to create a WordPress plugin without it showing up in your plugins list? This tutorial will show you a way to create and activate plugins from within a WordPress theme, without having them show in the admin dashboard.

Why would you want to do this though? Well it’s especially useful in situations where you only want a plugin to be available to a specific theme, such as when you’re running a multisite setup and don’t want to disrupt any of the other active sites. If there’s an error in your plugin, it will only show up in that theme, and not affect any other sites.

The tutorial will convert a basic boiler-plate template into an isolated theme template as an example to show how you could go about converting existing templates.

Step 1: Create our theme’s plugin folder

First we’ll create a plugins folder inside our theme directory, where all our plugins can go. I’m just using the default twentyfourteen theme here:

Directory structure

Because we’ll be referring to the location of this folder often (if you have lots of plugins within your theme), it’s useful to define a couple of constants in our functions.php file:

define("THEME_PLUGIN_DIR", get_stylesheet_directory().’/plugins/‘);

define("THEME_PLUGIN_URI", get_stylesheet_directory_uri().’/plugins/');

The first constant is our theme directory path, and the second is the url path to the plugin. We’re using get_stylesheet_directory() as it points to the top level of our theme, then appending /plugins, the name of our plugin folder.

Step 2: Converting a plugin into an isolated theme plugin

To demonstrate how to convert a plugin to a theme plugin, I’ll use my Wordpress boiler plate plugin, adapted from mr johnson. The plugin has a simple hello world action hook, which I’ve put inside my page.php file. So when the plugin is activated, the hello world message will be visible on all pages.

Drop the boiler plate plugin into your theme’s plugins folder, then we’ll make the magic happen.

Directory structure 2

Step 3: Register our theme’s plugin inside functions.php

For each plugin we want to register within our theme, we need to add a line into functions.php. To register the boiler plate plugin, just add:

require_once( THEME_PLUGIN_DIR.'/bioler-plate/boiler-plate.php' );

To disable this plugin, all you'd need to do is comment out the above line. The plugin won’t actually work just yet though, we need to make a few simple changes inside a couple files, as covered in the next step.  

Step 4: Change the directory paths  

To make the plugin work, we need to make changes in two files: boilerplate.php, and class-boiler-plate.php. Firstly, we need to change anywhere using plugin_dir_path, or plugin_dir_url to our theme plugin directory: THEME_PLUGIN_DIR or theme url (THEME_PLUGIN_URL) respectively. For example, in boiler-plate.php change the following:

/**  
 * The core plugin class that is used to define internationalization,
 * dashboard-specific hooks, and public-facing site hooks.
 */
require plugin_dir_path( __FILE__ ) . ‘public/class-boiler-plate.php';

Becomes:

require  THEME_PLUGIN_DIR.'boilerplate/public/class-boiler-plate.php';

Similar changes are required in class-boilerplate.php, for example:

public function enqueue_styles() {  
                wp_enqueue_style( $this->plugin_name.'-public', plugin_dir_url( __FILE__ ) . 'css/public.css', array(), $this->version, 'all' );
    }

becomes:

public function enqueue_styles() {  
                wp_enqueue_style( $this->plugin_name.'-public', THEME_PLUGIN_URL . ‘boilerplate/css/public.css', array(), $this->version, 'all' );
    }

Step 5: Activate the plugin on theme setup event

This is probably the most important step. When the boiler plate plugin is activated in the normal way, it actually loads from this function:

add_action( 'plugins_loaded', array( 'Boiler_Plate', 'get_instance' ) );  

All we have to do is change plugins_loaded to after_setup_theme. This way, the plugin is automatically activated when our theme loads.

That’s it really! To take it a step further, if you want certain functions to only run on certain pages, you can carry out basic checks in the class-boilerplate.php function such as:

public function enqueue_styles() {

if(is_home()){  
                wp_enqueue_style( $this->plugin_name.'-public', THEME_PLUGIN_URL . 'css/public.css', array(), $this->version, 'all' );
 }
}

This, for example, will ensure your stylesheet is only loaded on the homepage.