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_footerruns just before</body>on every front-end page of a classic or block theme.manage_optionslimits the output to administrators. Server details are not something to show to every visitor.$_SERVER['SERVER_PORT']is turned into a whole number withabsint(), and the finished line goes throughesc_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 firstadd_action(). - Let editors see it too: change
manage_optionstoedit_pages. - Style it in your stylesheet with the
dpc-server-portclass and remove the inlinestyleattribute.
Gotchas
- Behind a reverse proxy, Docker or a load balancer,
SERVER_PORTis the port PHP was reached on inside the stack, often80, not the public port in the browser. The proxy usually passes the public one in anX-Forwarded-Portheader. - If
is_ssl()sayshttpon an HTTPS site, the proxy does not tell WordPress about HTTPS. That causes redirect loops and mixed content, and it is fixed inwp-config.phpor 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.