How to Show a Different WordPress Theme based on IP Address [SOLVED]

January 25, 2019

A colleague and I recently stumbled on a situation where we had to show one wordpress theme to the web site visitors but show a different theme to ourselves so we can develop the new theme on the live site but not showing the development theme to the web site visitors.

A search on uncle google did not yield any decent results and we did not want a bloaty third party plugin slowing things down and in the end we figured that the best way will be to filter our ip’s and switch the theme based on ip address.

So we spun up a quick, plugin and used the switch_theme function in wordpress.

BOOM! Now when we visit the clients site I can see the new theme and see how it interacts with the current content without interrupting the user experience. NICE!

Here is the code below which help us accomplish the task.

All you have to do is:

  1. Create a file inside your plugins folder called “change-the-theme.php”
  2. Drop the code below into that file
  3. Add the ip addresses to the list of developers ip’s
  4. Change the theme names
<?php
/*
Plugin Name: Change Theme by IP Address
Description: Changes the WordPress theme depending on the visitor's IP address
*/

//our developers ip's
$developers_ips = array(
"255.255.255.255",
"255.255.255.254",
);

//check if the visitors ip is inside the list of developer ip's
if(in_array($_SERVER['REMOTE_ADDR'], $developers_ips)){

        //theme visible to developers
	switch_theme( 'the-new-theme-for-developers' );

} else{

        //theme that's visible to the public
	switch_theme( 'the-public-theme' );
	
}
?>