Conditional Frontend Themes for Users/Roles

Learn how to use conditional logic to load different themes for specific users on your WordPress site. This is a useful bit of code if you’re developing a new theme for an existing site, and it’s not quite ready for production yet. You can set things up so that you (the developer and site administrator) see the new theme that you’re working on, but all other visitors see the site’s standard theme.

Most of the tutorials on this site use little PHP files in your custom child theme to implement new functionality. But we can’t do that here, because we need to intercept WordPress BEFORE it decides which theme to apply. So, we need to lash together a little custom plug-in to do the job for us.

Child themes based on Astra

Create a Very Simple WordPress Plugin

We could create a full-on, well structured, object-oriented plug-in, using something like the WordPress Plugin Boilerplate Generator. It’s a great tool to get started writing a plugin, but it can be overkill for very small projects like this one. All we really need to do here is create a folder with a single PHP file in it. A super-simple little plugin 😎

On your computer, create a folder called my-theme-divertor and create text file in it, called my-theme-divertor.php. Open this text file and paste the following into it to get started.

<?php

/**
 * Plugin Name:  My Theme Divertor
 * Plugin URI:   https://wp-tutorials.tech/refine-wordpress/conditional-themes/
 * description:  Conditional frontend theme divertor
 * Version:      1.0.0
 * Author:       John Doe
 * Author URI:   https://headwall-hosting.com/
 * Text Domain:  my-theme-divertor
 */

// If this file is called directly, abort.
if (!defined('WPINC')) {
   die;
}

// Our code will go here...

This is enough scaffolding to get our plugin recognised by WordPress. So you can upload and install the plugin.

  • Zip up the my-theme-divertor folder so you get a file called my-theme-divertor.zip
  • On your website, go to the plug-ins admin page
  • Click on Add New and then Upload Plugin
  • Upload and activate your zip file.

infoFor more information on writing plugins, check out our “Create a Simple Custom WordPress Plugin” tutorial.

Edit WordPress plugin code

When the plugin’s installed and activated, everything should work as it did before. Your website should still be working normally, and we’ve got a PHP file where we can add our conditional logic.

You can edit the plugin code directly in the WordPress admin area, which is fine for the small edits we need to make.

Add the Conditional Theme Switching

We’re going to make a simple function that returns either true or false, telling us whether or not to redirect WordPress to our temporary/dev theme. This function might look a bit… convoluted – you could rewrite this in a single line. But, I like code to be really easy to read in a step-by-step way by people who might not be seasoned coders.

function is_theme_divert_required() {
	$is_divert_required = false;

	if (is_admin()) {
		// ...
	} elseif (!function_exists('wp_get_current_user')) {
		// ...
	} elseif (!current_user_can('administrator')) {
		// ...
	} else {
		$is_divert_required = true;
	}

	return $is_divert_required;
}

Now all we need to do is put this into a function called is_theme_divert_required() and call it in a couple of filter handlers.

add_filter('template', 'mtp_alternative_template', 50);
add_filter('stylesheet', 'mtp_alternative_stylesheet', 50);

Putting it All Together

Here’s the entire lump of code that sits in the my-theme-divertor.php file. You can copy-and-paste this and use it as-is. Just change the value of MTP_ALTERNATIVE_THEME_NAME to the theme you want to use.

<?php

/**
 * Plugin Name:  My Theme Divertor
 * Plugin URI:   https://wp-tutorials.tech/refine-wordpress/conditional-themes/
 * description:  Conditional frontend theme divertor
 * Version:      1.0.0
 * Author:       John Doe
 * Author URI:   https://headwall-hosting.com/
 * Text Domain:  my-theme-divertor
 */

// If this file is called directly, abort.
if (!defined('WPINC')) {
	die;
}

// Replace this with the slug for your alternative/dev theme.
const MTP_ALTERNATIVE_THEME_NAME = 'twentytwentyone';

function is_theme_divert_required() {
	$is_divert_required = false;

	if (is_admin()) {
		// ...
	} elseif (!function_exists('wp_get_current_user')) {
		// ...
	} elseif (!current_user_can('administrator')) {
		// ...
	} else {
		$is_divert_required = true;
	}

	return $is_divert_required;
}

function mtp_alternative_template($template) {
	if (is_theme_divert_required()) {
		$template = MTP_ALTERNATIVE_THEME_NAME;
	}

	return $template;
}
add_filter('template', 'mtp_alternative_template', 50);

function mtp_alternative_stylesheet($stylesheet) {
	if (is_theme_divert_required()) {
		$stylesheet = MTP_ALTERNATIVE_THEME_NAME;
	}

	return $stylesheet;
}
add_filter('stylesheet', 'mtp_alternative_stylesheet', 50);

That’s it. Have fun diverting different users to different themes.

Leave a Comment

Your email address will not be published. Required fields are marked *