WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there a WP function to automatically get the correct URL of the current page? Meaning if I just opened a single post, the function returns the same as get_permalink(), but if I'm on a paginated instance of a page (when paginating through the comments), the function returns the same as get_pagenum_link(get_query_var('paged')) would do.

I've searched the codex but didn't find what I was looking for. (But even get_pagenum_link() isn't documented there.)

I know about this function already, but I would be glad if there was a "native" WP function that does the job.

Thanks in advance! Regards, René

share|improve this question

In addition to Rajeev Vyas's answer, you don't need to pass any non-empty parameters to add_query_arg(). The following has always worked well for me:

# relative current URI:
$current_rel_uri = add_query_arg( NULL, NULL );

#absolute current URI:
$current_uri = home_url( add_query_arg( NULL, NULL ) );

The function falls back on $_SERVER[ 'REQUEST_URI' ] and applies urlencode_deep() to it. See https://github.com/WordPress/WordPress/blob/3.8/wp-includes/functions.php#L673

Edit: As $_SERVER[ 'REQUEST_URI' ] represents unfiltered user input, one should always escape the return value of add_query_arg() when the context is changed. For example, use esc_url_raw() for DB usage or esc_attr() or esc_url() for HTML.

share|improve this answer
global $wp;
$current_url = add_query_arg( $wp->query_string, '', home_url( $wp->request ) );

Not a function, but definately using wordpress code..


share|improve this answer
I'll give this a try, thanks. – René Schubert Feb 1 '13 at 10:01
It works partly, but some things don't work like I want them: Parameters like year are added to the query string, even though they haven't been there before, and other parameters, for example the replytocom parameter, get lost. Do you know a solution for this? – René Schubert Feb 1 '13 at 13:32

1) $_SERVER['REQUEST_URI'] - It return the URL in to access the page which is executing the script. If you need to type http://www.example.com/product.php?id=5 to access the page then $_SERVER['REQUEST_URI'] returns /product.php?id=5.

2) $_SERVER['DOCUMENT_ROOT'] – Returns the root directory of the server which is specified in the configuration file of server. This variable usually returns the path like /usr/yoursite/www in Linux and D:/xamps/xampp/htdocs in windows.

3) $_SERVER['HTTP_HOST'] – Returns the host’s name as found in the http header. This variable usually returns the path like example.com when the you find http://example.com in browser’s address-bar and return www.example.com when you see http://www.example.com in the address-bar. This is quite useful when you’ve to preserve session while making online payment using PHP since session stored for http://example.com is not same as for the http://www.example.com.

4) $_SERVER['HTTP_USER_AGENT'] - Returns the user agent’s (browser) detail accessing the web page. We can use strpos($_SERVER["HTTP_USER_AGENT"],”MSIE”) to detect Microsoft Internet explorer or you can use strpos($_SERVER["HTTP_USER_AGENT"],”Firefox”) to detect firefox browser in PHP.

5) $_SERVER['PHP_SELF'] - Returns the file-name of the currently executing script. Let’s suppose that you’re accessing the URL http://www.example.com/product.php?id=5 then $_SERVER['PHP_SELF'] returns /product.php in your script.

6) $_SERVER['QUERY_STRING'] – Returns the query string if query string is used to access the script currently executing. Query strings are those string which is available after “?” sign.if you use $_SERVER['QUERY_STRING'] in the script executing the following URL http://www.example.com/index.php?id=5&page=product then it returns id=5&page=product in your script.

7) $_SERVER['REMOTE_ADDR'] – Returns the IP address of remote machine accessing the current page. But you can’t relie on $_SERVER['REMOTE_ADDR'] to get the real IP address of client’s machine. See this article to know how to get real IP addrees in PHP.

8 ) $_SERVER['SCRIPT_FILENAME'] - Returns the absolute path of the file which is currently executing. It returns path like var/example.com/www/product.php in Linux and path like D:/xampp/xampp/htdocs/test/example.php in windows.

share|improve this answer
Keep in mind that none of those are available in your CLI and therefore useless for cron jobs. – kaiser Feb 26 '14 at 11:38

wp_guess_url is what you are looking for.

Guess the URL for the site.

Will remove wp-admin links to retrieve only return URLs not in the wp-admin directory.

share|improve this answer
This returns a totally different URL other than the current page when I tried using it. – Kirby May 19 '15 at 16:10

I dont now of pagination but You can use this function to get url within the loop

<?php $ID = get_the_ID(); 
echo get_permalink( $ID ); ?>

Or else if you dont prefer to use php you can also opt for jquery method here (this will help you to make it work outside the loop)

$(document).ready(function () {
var vhref = $(location).attr('href');
var vTitle = $(this).attr('title');
$('#spnTitle').html('' + vTitle + '');
$('#spnURL').html('' + vhref + '');

or if u prefer to use php function you need to get the id outside the loop

share|improve this answer
Sorry, but this is just not what I've asked for... + the_permalink() doesn't need the id when it's called inside the loop. – René Schubert Feb 1 '13 at 10:04
This answer had very little to do with the original question. It didn't answer it correctly nor did it really give any valuable information concerning the topic. – jounileander Jul 4 '13 at 8:22

You can use wordpress function to get current page URL


This will return you the curremt page URL link.

share|improve this answer
Almost; the_permalink() will print the URL of the current post. But it won't work for other requests, and won't include variables like pagination. So not really the answer @René Schubert is looking for. – TheDeadMedic Feb 26 '14 at 11:10

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.