Screenshot of the localhost plugin in action

Have you ever been infuriated when setting up your hosts file to use your local IP and forgotten which IP you were on? Then have you accidentally done something silly to your production environment? The following Mu plugin can help alleviate that problem by displaying a brightly coloured label in the bottom right hand corner of your screen whenever you are using your localhost. The red label displays text showing the name of the current server.

Installation

To use this plugin, you will need to create an mu-plugins folder in your WordPress installation. Then simply create a .php file in it and copy and paste the following code inside. Note, that if your localhost uses an IP other than 127.0.0.1 then you will need to edit that code in the plugin file. You can style the button however you wish by editing the inline styling.

<?php

/*
 * Adds a bright red box on localhost
 * Box contains the server name
 *
 * @author Ryan Hellyer <ryanhellyer@gmail.com>
 */
function ryans_localhost() {

	// Do check for localhost IP (remove this if you want to ALWAYS display it)
	if ( '127.0.0.1' != $_SERVER['REMOTE_ADDR'] && '::1' != $_SERVER['REMOTE_ADDR'] ) {
		return;
	}

	// Alert user to whether WP_DEBUG is on or not
	if ( defined( 'WP_DEBUG' ) && WP_DEBUG == true ) {
		$debug = 'WP_DEBUG=ON';
	} else {
		$debug = 'WP_DEBUG=OFF';
	}

	// Output the code to the page
	echo '
		<div style="
			position: fixed;
			right: 10px;
			bottom: 10px;
			width: auto;
			padding: 0 8px;
			height: 22px;
			background: #ff0000;
			border-radius: 5px;
			box-shadow: 0 2px 5px 2px rgba(0,0,0,0.3);
			z-index: 99999999999999;
 
			font-family: sans-serif;
			font-size: 13px;
			line-height: 22px;
			color: #fff;
			font-weight: bold;
			text-align: center;
			text-shadow: 1px 1px 1px rgba(0,0,0,0.3);
		">' . php_uname( 'n' ) . ' ' . $debug . '</div>';
}
add_action( 'wp_footer', 'ryans_localhost' );
add_action( 'admin_footer', 'ryans_localhost' );

?>

LAQ (Likely to be Asked Questions)

  • Q:Where’s the zip file? Plugins are supposed to have zip files!A: This is intended for developers only. They’re quite capable of copy and pasting the code themselves.
  • Q: I don’t get it, what’s a hosts file?
    A: Something used for telling your OS where to look for a website at. It’s a handy thing to edit when working on development versions of websites.
  • Q: Why not make it a regular plugin? That way we could get updates from WordPress.org. Also, don’t go telling people to edit plugin files, that’s a bad idea!
    A: Updates are irrelevant. This is intended for developers. They should hack it up however suits them best.

Credits

This is based on a concept developed by Kaspars Dambis. I couldn’t find the code he wrote originally, so rewrote it and dumped it here for everyone to use 🙂 Thanks also to Ronald Huereca for posting an improvement in the comments below, which caters for IPV6 and Aaron Campbell for the tip regarding displaying the server name.