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

2 Answers 2

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

4
  • how I appreciate your expressions, I was wanting to still load a theme from a plugin.
    – samjco-com
    Commented May 4, 2018 at 3:30
  • let me try to say it differently, the system is not designed to work this way Commented May 4, 2018 at 5:01
  • If you haven't notice. Many new WP builders use this method to remove default themes and use their own using a plugin
    – samjco-com
    Commented Dec 24, 2022 at 11:40
  • @samjco not sure why you bring it after 4 years. that was true and wrong when you asked the question the first time. Meanwhile there was gutenburg that killed separation of content and styling so maybe the discussion is pointless now. Commented Dec 25, 2022 at 16:12
0

So after tinkering a bit, I came up with 2 solutions.. Add into your main plugin init php file:

$enable_theme = true; //conditional

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;

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

Or by using an WP action

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 and acknowledge you have read our privacy policy.

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