In order to dynamically switch a theme in functions file (e.g. when wanting a different theme for a different domain, but same website), you use the following:

add_filter('template', 'my_switch_theme_for_domain');
add_filter('option_template', 'my_switch_theme_for_domain');
add_filter('option_stylesheet', 'my_switch_theme_for_domain');

function my_switch_theme_for_domain()
    return 'my-child-theme';

This does not seem to work when wanting to call a child theme. I get a white/blank page when calling a child theme, I have tried a few.

Is there some other code to switch to a child theme?

  • Your function returns a string. Your string is 'my-child-theme'. – Max Yudin Oct 28 '19 at 15:51
  • Yes that’s correct. Works fine if the theme is a main theme and not a child theme. – Laurence Cope Oct 28 '19 at 15:52
  • Can't understand the logic with three WordPress filters and empty custom function involved. – Max Yudin Oct 28 '19 at 16:24
  • These filters will switch to theme returned in the function – Laurence Cope Oct 28 '19 at 16:29

For a parent theme, template and stylesheet should be the same, which is the name of the parent theme directory.

But for a child theme, template should be the parent theme's directory name, whereas stylesheet is the child theme's directory name.

So if you've got the following:

  • Parent theme: Twenty Nineteen (directory name: twentynineteen)

  • Child theme: My Child Theme (directory name: my-child-theme) which uses the Twenty Nineteen theme as its parent.

And you want to set the child theme as the active theme using the hooks in question, then your code would look like so:

(You can combine the functions, but in this answer, I intentionally make them independent..)

// For the parent theme.
add_filter('template', 'my_switch_theme_for_domain');
add_filter('option_template', 'my_switch_theme_for_domain');
function my_switch_theme_for_domain() {
    return 'twentynineteen';

// For the child theme.
add_filter('stylesheet', 'my_switch_theme_for_domain2');
add_filter('option_stylesheet', 'my_switch_theme_for_domain2');
function my_switch_theme_for_domain2() {
    return 'my-child-theme';

But if you wanted to set twentynineteen (the parent theme) as the active theme using those hooks, then both the above functions should return twentynineteen which means the theme stylesheet (and other files) are to be loaded from the twentynineteen directory only.

I hope that makes sense.. :)

| improve this answer | |

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.