Here I shared a code that you can use to switch the WordPress theme to your custom theme. Only the name of the theme is required in this code to make your theme always the selected one for visitors.
function set_my_custom_theme() {
//replace your theme name
return 'your-theme-name';
}
add_filter( 'template', 'set_my_custom_theme' );
add_filter( 'stylesheet', 'set_my_custom_theme' );
Above code explain
the template and stylesheet filters are responsible to select the activated theme for the visitors.
In line 3 of the above code, we set the returning theme name for visitors to your-theme-name
which you can change to your desired theme name.
Copy this code to the PHP file of your custom WordPress plugin to use it.
Why would I want to Switch the Active WordPress Theme?
This function will be useful if you desire to display different themes to different types of users.
For example, if you want to change the displaying theme for logged-in users. Or maybe each WordPress user or user role needs different functionalities available in another theme. You can use this function to switch the WordPress theme for them.
written by Mehdi Nazari about in WordPress WordPress Functions WordPress Theme Development
What do you think about "How to Switch Theme in WordPress Programmatically"?