Who Can Tell Me Where I Am?

The other day I was writing plugin code and I thought it would be useful to know the URL of the page a visitor is on.

Back in the day, I would get the user’s current URL with something like:

$url = 'http://' . $_SERVER['HTTP_HOST']  . $_SERVER['REQUEST_URI'];

Instead of going with how I used to do it, I thought it would be better to try the WordPress way. There’s a function for that, right?

Well, after a quick search, I found get_permalink(), but after reading the specs, I realized that function only works with single posts and doesn’t reveal the URL of all locations in a WordPress site.

I then discovered get_bloginfo() and, when I saw the list of possible values for the $show parameter, I thought I was onto something. Unfortunately, I didn’t see any parameter value that would help.


Given my searching around wasn’t paying off, I decided I would just browse through the main WordPress API – the functions.php file. It’s a long file – 5,291 lines, but worth the effort to review.

I didn’t find what I was looking for, but I did find several useful and a few interesting functions. The wp_find_hierarchy_loop_tortoise_hare() is  my favorite, though I will probably never use it.

Seeing that functions.php lives in wp-includes, I decided next to browse through that folder to see what I could find.


The next file I reviewed was http.php. This file was much shorter – only 669 lines, but harder to understand how I’d use it. I think the answer I’m looking for may be available through one of those functions, but I couldn’t see it and, honestly, I was looking for something more straightforward. Something like get_current_url().


There are a lot of files in the wp-includes folder. I also skimmed through load.php, plugin.php, and query.php, and never did find what I was looking for, though I do want to go back and look at query.php more closely.

Given how load.php has a function called wp_fix_server_vars() to “Fix `$_SERVER` variables for various setups,” I’ve come to the conclusion that the WordPress way, in this case, may be to stay old school and just do this the way I used to.

Well, I guess I could get a little clever and do the following:

$url  = get_bloginfo( 'url' ) . $_SERVER['REQUEST_URI'];

but that just seems unnecessary.