How to Get Current Page URL in WordPress

If you’re looking for a method to get the current page URL in WordPress, then I am sure that you’re developing something unique in WordPress but stuck because none of the codes you found are working as per your expectation.

Luckily for you, there are various ways to get the current page URL in wordpress, and I am sharing all of those ways along with their difference from one another so that you can use the one best suited for your need.

So let’s get further in our article and find out the ways available in WordPress to get the current page URL.

Methods to Get Current Page URL in WordPress

There are several ways to get the current URL of a page in WordPress, but I believe the two most commonly used ones are: getting the URL without query strings (?foo=bar) and getting the entire URL along with query strings.

It’s up to you to choose between the two based on your requirements.

1. Get Current Page URL in WordPress Without Query Strings

In most cases, the current page URL can be retrieved with the home_url() function. Copy and paste the code given below into the file where you want to obtain the current URL of the page, and it will show the WordPress current page URL.

Here’s the WordPress PHP code which you can use to get the current page URL:

global $wp;
echo home_url( $wp->request );


The global $wp; variable holds the value of the current WordPress instance.

Whereas the $wp->request includes the path of the URL, e.g., /path/to/page, and the home_url() function is used to output the homepage URL which is set in Settings > General. A variable with the path value can also also be passed to the home_url() function to combine it at the end, as we are doing in the code above.

The echo home_url( $wp->request ); is used to print the current page URL of your site.

This is one of the most preferred ways of getting any page URL in WordPress. However, if your permalink is set to plain, then this code will not work correctly as it strips out the query string present at the end of the URL, which is anything starting with “?” in the URL, for e.g., ?foo=bar.

So if you also need to retrieve the query string which is present in your URL, then you can use the next method.

2. Get Current Page URL in WordPress With Query Strings

This method is a little bit complex as compared to the first one due to the fact that this method also provides the query string present in your current URL. However, this is a foolproof way of getting any page URL irrespective of the permalink setting.

To get the current page URL along with the query string, you can use the WordPress PHP code given below:

global $wp;
echo add_query_arg( $wp->query_vars, home_url( $wp->request ) );


There are many functions in this code that have already been explained in the previous method. Aside from those, the add_query_arg() function used in this code is used to add query arguments passed in it with a URL, while $wp->query_vars returns the query string available in the current page URL.

So when both the current page query string and homepage link are passed to the add_query_arg() function, it combines them and provides you with the complete link to the page.

Some of you might be wondering why I used $wp->request in this piece of code, this is done to make the function more reliable. By using this code, you can get URLs with not only query strings but also URLs that have both query string and path, like

With this piece of code, you can get the URL of any WordPress page, whether it is a single page, post page, or any other page that exists in the scope of WordPress.

Other Ways to Current Get URL in WordPress (Template Hierarchy Specific)

There are some other ways that you can use in order to get the current page URL in WordPress, but unlike like the ways that are mentioned above in the article, these methods are specific to different parts of WordPress template hierarchies.

✍️ Author’s Note

The below method will only work in different parts of the WordPress hierarchy, so if you don’t know what the WordPress template hierarchy is or else if you want to use the current URL code on the entire website or various parts of your website, then this section of the article is not meant for you.

If you want to get the current WordPress page URL on the single.php or post.php template file, you can use the get_permalink() function provided by WordPress.

$obj_id = get_queried_object_id();
$current_url = get_permalink( $obj_id );

If you want to get the current URL on any category or tag page (i.e., taxonomy.php, category.php, tag.php, etc.) for that, you can use the get_term_link() function, which will provide you with the URL of the tag or category when its unique ID is provided in the function.

$obj_id = get_queried_object_id();
$current_url = get_term_link( $obj_id );

If you’re looking to get the URL of the author page (i.e., author.php) then you can use the get_author_posts_url() function.

$obj_id = get_queried_object_id();
$current_url = get_author_posts_url( $obj_id );

Lastly, in order to get the URL of front-page.php or home.php, you don’t even need to use any of these complicated codes. You can simply use:

$current_url = home_url( '/' );

In most of these methods, the get_queried_object_id() function is used to retrieve the ID of the currently queried object.

Frequently Asked Questions

Here are some of the questions that actually came into my mind, and I thought that I might as well include them here to help you out.

Can I use the get_permalink() function to get the current page URL in WordPress?

Yes, the get_permalink() function returns the URL of the current page, and hence you can use it to get the current page URL in WordPress. However, it will only work on single pages & posts and will only work inside the loop.

Can I Use PHP superglobal variable $_SERVER to get the current page URL in WordPress?

Yes, you can use the superglobal $_SERVER PHP variable to get the current page URL in WordPress. However, when coding in wordpress, it is recommended to use the functions which are provided by WordPress.

How to use the $_SERVER PHP variable to get the current page URL in WordPress?

Here’s the code to get current page URL using $_SERVER PHP variable.

$actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
echo $actual_link;

Wrapping Up

I hope that this article was helpful in getting the correct URL for your current page. I tried my best to provide you with all the different WordPress PHP codes available that can help you to achieve your goal.

Do not hesitate to comment below if you face any difficulty with the methods while going through my article. Stay tuned to my blog for more such articles and tutorials on WordPress.

Leave a Comment