How do I get from search_theme_directories() to the $stylesheet (name) for switch_theme()

But I can’t seem to sort out how to get the $stylesheet. Easy. $themes = search_theme_directories(); // a random theme $themename = array_rand($themes,1); // that random theme’s stylesheet $stylesheet = content_url(‘/themes/’.$themes[$themename][‘theme_file’]); // The absolute path to that theme’s stylesheet $stylepath = WP_CONTENT_DIR.’/themes/’.$themes[$themename][‘theme_file’]; Getting a random theme that is not a child is more involved. I … Read more

Switch theme if ie compatibility/quirks mode?

If you’re theme is working fine in IE 6, 7, 8 & 9 with compatibility mode disabled then you don’t really need to have it enabled. If you (try to) respect web standard, you can simply always use http-equiv=”X-UA-Compatible” content=”IE=edge”. To keep compatibility with old browsers, just avoid using latest web features: use the subset … Read more

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 … Read more

How to switch theme if the current user is admin?

You are sort of doing this in a roundabout way. WordPress has a function called switch_theme(): add_action( ‘setup_theme’, ‘switch_user_theme’ ); function switch_user_theme() { if ( current_user_can( ‘manage_options’ ) ) { switch_theme(‘twentytwelve’); } else { switch_theme(‘twentythirteen’); } } The argument is the directory of the theme you want. I can’t help but think this is a … Read more

How to display message (with switch_theme hook) after deactivating My theme?

It’s impossible, because after deactivation your theme isn’t even loaded! It’s possible, but hacky. Essentially, we unset the action param, load in the themes admin page & then exit before the redirect-on-success occurs. add_action( ‘switch_theme’, ‘wpse_60972_theme_deactivate_message’ ); function wpse_60972_theme_deactivate_message() { $msg = ‘ <div class=”error”> <p>Your theme has been DEACTIVATED</p> </div>’; add_action( ‘admin_notices’, create_function( ”, … Read more

Switching content between summer and winter

Probably the easiest thing to use is the date() function with the “daylight savings argument and add a custom <body> class: <?php /** Plugin Name: (#179112) Daylight savings body class */ add_filter( ‘body_class’, ‘daylightsavings_classes’ ); function daylightsavings_classes( Array $classes ) { $classes[] = 0 === date(‘I’) ? ‘winter’ : ‘summer’; return $classes; } This plugin … Read more