Load-balanced WP installs: which server am I looking at?

Been working on a few WordPress installs on load-balanced servers recently. Load balancing is a great technology and has so many benefits to a site that gets overloaded a lot, but I had a few contributors and editors that were not seeing up-to-date images. The front end servers sync files between themselves, but it can take a moment or two - more than long enough for people to panic.

I tried a few different load balancing settings but some users were using one machine to edit posts and another to check it. So there was no way for them to see which server they were on.

During development, I had added the hostname to the <head> in a comment…

<!-- hostname: master.example.com -->

… but that was only really available to users who know their way around developer tools or HTML. I needed something else.

A while back, I had been playing with the admin bar in WP (see Getting rid of Howdy) and thought I could add the hostname to the user-info dropdown. I came up with the following:

// add server hostname to user-info in admin bar
function boswall_add_hostname() {
global $wp_admin_bar;
$user_info_node = $wp_admin_bar->get_node('user-info');
'id' => 'user-info',
'title' => $user_info_node->title . '<span class="username">' . exec("hostname") . '</span>'
add_action( 'wp_before_admin_bar_render', 'boswall_add_hostname' );

So now I can tell users they need to check the server they are editing on and the server they are viewing on. If they are different, then it might take a few moments to update.

Now you know where you are.