Show the Server Port in the WordPress Footer

Sometimes you need to know which port your WordPress site is served on: a local setup on :8080, a staging server behind a load balancer, or a host that moved you to a new stack. This snippet prints the server port in the WordPress footer, together with whether the request was http or https.

Only administrators see the line. Visitors, search engines and logged-in customers get the normal page, so you can leave it on while you debug a live site.

The snippet

<?php
/**
 * Show the web server port (and http/https) in the front-end footer.
 * Only administrators see it.
 */
add_action( 'wp_footer', 'dpc_print_server_port' );

function dpc_print_server_port() {
	if ( ! current_user_can( 'manage_options' ) ) {
		return;
	}

	$port = isset( $_SERVER['SERVER_PORT'] ) ? absint( wp_unslash( $_SERVER['SERVER_PORT'] ) ) : 0;
	if ( ! $port ) {
		return;
	}

	printf(
		'<div class="dpc-server-port" style="margin:20px 0;text-align:center;font:13px/1.5 monospace;">%s</div>',
		esc_html(
			sprintf(
				/* translators: 1: server port number, 2: http or https. */
				__( 'Server port: %1$d (%2$s)', 'dpc' ),
				$port,
				is_ssl() ? 'https' : 'http'
			)
		)
	);
}

Add it with Scripts Organizer

In Scripts Organizer go to Code Blocks → Add New and give the block a name. Set Trigger location to Everywhere, tick PHP under Script location, paste the code into the PHP editor and publish. Leave Action/Hook empty.

The code only registers a wp_footer callback. current_user_can() is called inside it, when WordPress has loaded the user. When you are done, disable the code block instead of deleting it, so you can switch it back on next time.

The same code also works unchanged in a small plugin or an mu-plugin.

How it works

  • wp_footer runs just before </body> on every front-end page of a classic or block theme.
  • manage_options limits the output to administrators. Server details are not something to show to every visitor.
  • $_SERVER['SERVER_PORT'] is turned into a whole number with absint(), and the finished line goes through esc_html(). The older version of this post echoed the raw value.
  • is_ssl() tells you whether WordPress thinks the request is HTTPS, which is the next question when a port looks wrong.

Customise it

  • Show it in wp-admin as well: add add_action( 'admin_footer', 'dpc_print_server_port' ); below the first add_action().
  • Let editors see it too: change manage_options to edit_pages.
  • Style it in your stylesheet with the dpc-server-port class and remove the inline style attribute.

Gotchas

  • Behind a reverse proxy, Docker or a load balancer, SERVER_PORT is the port PHP was reached on inside the stack, often 80, not the public port in the browser. The proxy usually passes the public one in an X-Forwarded-Port header.
  • If is_ssl() says http on an HTTPS site, the proxy does not tell WordPress about HTTPS. That causes redirect loops and mixed content, and it is fixed in wp-config.php or the server config, not here.
  • Full-page caches store the page for logged-out visitors, so the admin-only line never ends up in a cached page. If your cache also serves logged-in users, exclude administrators.

In the Scripts Organizer library Import it with one click from Scripts Organizer.

// newsletter

Three tested WordPress snippets a week

Blocks, security, performance, Tailwind, PHP and React. Copy, paste, ship. You’ll also get DPlugins product updates. No spam, and you can unsubscribe anytime.

// newsletter

Three tested WordPress snippets a week

Blocks, security, performance, Tailwind, PHP and React. Copy, paste, ship. You'll also get DPlugins product updates. No spam, and you can unsubscribe anytime.