Can’t switch to a child theme using filters template, option_template and option_stylesheet

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.. 🙂