0

I want to activate a theme located in my plugin as the main theme.

/*
 * For directory structure like:
 * 
 * /my-plugin/
 * - /my-plugin.php
 * - /themes/
 *
 * 
 */
register_theme_directory( dirname( __FILE__ ) . '/themes' );

What am I missing to then activate the theme on plugin activation?

https://codex.wordpress.org/register_theme_directory

0

You are missing everything. Themes are not plugins and should not depend on plugins being active. The admin user is the only one that should to activate whichever themes and plugins that he want in whatever order he want.

If your theme requires a plugin to operate you better distribute it as part of the theme

  • how I appreciate your expressions, I was wanting to still load a theme from a plugin. – samjco May 4 '18 at 3:30
  • let me try to say it differently, the system is not designed to work this way – Mark Kaplun May 4 '18 at 5:01
0

So after tinkering a bit, I came up with this solution:

if($enable_theme == true):
   register_theme_directory( plugin_dir_path( __FILE__ ) . 'themes' );
   switch_theme( 'mythemename'); // my custom template/stylesheet name
else:
   switch_theme( 'default'); // template/stylesheet name
endif;

//Testing purposes
//$current_theme = wp_get_theme();
//$name = $current_theme->get_stylesheet();
//$path = $current_theme->get_stylesheet_directory();
//echo $name;
//echo $path;

OR

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() {
  if($enable_theme == true):
    register_theme_directory( plugin_dir_path( __FILE__ ) . 'themes' );
    $theme = "mythemename";
  else:
    $theme = "default";
  endif;

        updateTheme($theme);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.