Know when you are on localhost!
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.
Aaron D. Campbell says:
I often use something like this in the footer of my site: echo php_uname( ‘n’ );
This will display something like “aarons-desktop” on local and has the added benefit of showing which server in my cluster is serving a given page (useful when there seem to be sporadic issues and you’re wondering if it’s specific to a certain server). It’s more subtle, so I suppose it’s easier to miss, but you get used to it (you just start using the ‘end’ key more often in your work flow)
October 24, 2012 at 6:46 pm # //
Ryan Hellyer says:
That looks very useful! Thanks for the tip.
I might add that into my own code snippet.
October 24, 2012 at 6:48 pm # //
Archie22is says:
Awesome plugin bro! It does the much needed job 🙂
October 25, 2012 at 8:51 am # //
Ryan says:
Glad to hear you like it 🙂
November 15, 2012 at 3:16 pm # //
Ronald Huereca says:
I had to add an extra line for IPv6.
if ( '127.0.0.1' != $_SERVER['REMOTE_ADDR'] && '::1' != $_SERVER['REMOTE_ADDR'] )
Just FYI in case someone gets that for their REMOTE_ADDR.
October 29, 2014 at 7:55 pm # //
Ryan Hellyer says:
Thanks! I’ve never encountered that, but I’ll implement it into the posted code.
October 30, 2014 at 2:48 pm # //
Ronald Huereca says:
Yay! I don’t expect you to add this in, but I decided to add a WP_DEBUG notice to it also.
https://gist.github.com/ronalfy/2159dac987da8c51719a
October 30, 2014 at 3:00 pm # //
Ryan Hellyer says:
That makes complete sense too. And yep, I’ve added that in as well 🙂
October 30, 2014 at 3:58 pm # //