-2

I know that you can install plugin code as part of a theme, but I would like to do it the other way round. I'm developing a plugin that changes many aspects of WordPress and most will not be visible without my theme providing support for it. In other words it makes very little sense to run the plugin without the theme. I understand that it's not encouraged/allowed to do this in plugins hosted on wordpress.org, but my plugin is not distributed there and requires an administrator to upload a zip (which I take to mean they will have read the readme that explains the side effects).

It seems from this question that it's possible to use the plugin's activation function to copy the theme files to the necessary place and then activate it using switch_theme, but there are enough aspects of this method where something can go wrong (e.g. permission issues when copying the theme files) that I feel this is something better handled by WordPress core.

Ideally, I hope there is a way that I can provide the theme files as part of my plugin, so that I only have to manage one project, and the user only has to update one plugin.

1
  • 1
    I'd advise against this, if you can't bundle both in the same zip, and the plugin is on .org, then putting the theme on .org and prompting the user it's recommended within your UI would be better
    – Tom J Nowell
    Mar 6, 2018 at 19:23

2 Answers 2

1

Well instead of copying a theme over which would require file permissions, etc, you can add the themes files to the plugin folder and activate it from there:

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):

    /*** if the theme exists in your plugin file**/
    register_theme_directory( plugin_dir_path( __FILE__ ) . 'themes' );
    
    $theme = "mythemename"; // Other theme name to load

  else:

    $theme = "default";

  endif;

    updateTheme($theme);
}
0

It looks like the function register_theme_directory can be used for this purpose.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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