How to Bundle Theme(s) in a Plugin

Theme-in-a-Plugin

If you build a plugin that require template modification, it might be a good idea to bundle an example theme in your plugin. And here’s how to do it.

It’s very simple. WordPress have a function to register a themes directory register_theme_directory( $path );.

It’s as simple as that. So here’s the full plugin example:

<?php
/**
 * Plugin Name: f(x) Themes in Plugin Example
 * Plugin URI: https://shellcreeper.com/how-to-bundle-theme-in-a-plugin/ 
 * Description: Simple example to bundle theme(s) in a plugin.
 * Version: 1.0.0
 * Author: David Chandra Purnama
 * Author URI: https://shellcreeper.com/
**/

/**
 * Register Themes Directory
 * @link https://developer.wordpress.org/reference/functions/register_theme_directory/
 */
register_theme_directory( trailingslashit( plugin_dir_path( __FILE__ ) ) . 'themes' );

And then you simply need to create “themes” directory in that plugin and add your bundled themes there.

To make it easier, here’s the full plugin:

This example plugins include 3 blank themes, so you’ll see this in Themes page:

Theme-in-a-Plugin-Admin

 

IMPORTANT:
If you using Windows Server (Local such as XAMPP included), It will not work because there’s seem to be a bug related to Themes PATH/URI in Windows server. But this will work fine in most host.

I’ll report this bug soon-ish after some test.

 

1 Comment

  1. Mehrshad Darzi

    This code works correctly in the localhost (Xampp).
    your code is incorrect.
    you should write only folder Name (not full path).
    example :

    wp-admin
    wp-content->package->mytheme
    wp-includes

    This function is :
    register_theme_directory( ‘/package’ );

    Thanks a lot 🙂

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *