Is it possible to install a theme via a plugin?

I’d like to simplify the installation and default settings of my theme, by creating a plugin that installs it (versus the traditional “Appearance > Install Themes” routine. Once installed, I want to be able to delete the plugin from the site, so there will be no dependencies on it after activation.

Is this possible?

Read More

I would include the theme.zip file inside the plugin.zip file so that it could easily be transferred up and over to the themes directory.

If possible, any examples of this would be helpful. Thanks in advance for your help.

Related posts

Leave a Reply

2 comments

  1. Why not set the default settings from within the theme? You can use the ‘swith_theme’ action.

    To answer your question: yes, it’s possible, but it requires that the user’s setup allows moving files, which can lead to more headaches than covenience.

  2. Yes it is:
    https://wordpress.stackexchange.com/a/302650/29133

    /*
     * For directory structure like:
     * 
     * /your-plugin/
     * - /your-plugin.php
     * - /themes/
     * - - /yourthemename/
     *
     * 
     */
    

    Add to the your-plugin.php file:

    function updateTheme($theme){
        update_option('template', $theme);
        update_option('stylesheet', $theme);
        update_option('current_theme', $theme);
    }
    
    add_action( 'setup_theme', 'change_theme' );
    
    function change_theme(){
    
     //Replace with your own conditional
     $enable_theme = true; // Forcing true;
    
    
      if($enable_theme == true):
        register_theme_directory( plugin_dir_path( __FILE__ ) . 'themes' );
        $theme = "yourthemename";
      else:
        $theme = "default";
      endif;
    
            updateTheme($theme);
    }