Internet Explorer 6 is not supported.
Upgrade to view this site.

Current URL in WordPress

Posted on September 23, 2012 at 10:13 pm

This seems like it should be a given, but it seems that WordPress has no easy function to get the current URL. I didn’t want to use $_SERVER variables to get the URL, but after finding no good WordPress solution, I decided to bite the bullet and just make up a $_SERVER solution for the time being.

Some time passed, I forgot about it and found some other headaches to occupy my time. Then I found this post with a solution that was to my liking. The only thing that I didn’t like about it was that it always appended the original query string, whether you were using permalinks or not. I added a little conditional to it and now I have a current_url function that I wouldn’t kick out of bed.

request) ? home_url($wp->request) : add_query_arg($wp->query_string, '', home_url());
			return $trailingslash ? trailingslashit($current_url) : $current_url;

You must be logged in to post a comment.