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.

1
2
3
4
5
6
7
8
9
10
11
<?php
	if(!function_exists('current_url'))
	{
		function current_url($trailingslash = false)
		{
			global $wp;
			$current_url = isset($wp->request) ? home_url($wp->request) : add_query_arg($wp->query_string, '', home_url());
			return $trailingslash ? trailingslashit($current_url) : $current_url;
		}
	}
?>
Categories
Tags

Leave a Comment

*Required fields. Email will not be published.