17

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.

8 Answers 8

35

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 (on single site):
$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.

Update

The shown example that should create an absolute URI (containing scheme and host) does not work on multisite with sub-directories as home_url() would return the complete URI including a path segment. A better solution for multisite aware code would be this:

// absolute URI in multisite aware environment
$parts = parse_url( home_url() );
$current_uri = "{$parts['scheme']}://{$parts['host']}" . add_query_arg( NULL, NULL );

WordPress core does not support port, user or password in a multisite site URL so this should be sufficient.

1
  • Frustrating that OP did not select this answer as the Solution, which it is. Thank you.
    – cfx
    Aug 9, 2019 at 16:05
3
global $wp;
$current_url = add_query_arg( $wp->query_string, '', home_url( $wp->request ) );

Not a function, but definately using wordpress code..

http://kovshenin.com/2012/current-url-in-wordpress/

1
  • 2
    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? Feb 1, 2013 at 13:32
1

For me <?php esc_url(the_permalink()); ?> works (on a archive page with pagination).

2
  • Impossible. the_permalink() echoes the escaped permalink, it doesn't return anything. The esc_url() doesn't get anything, and if it would, it would be an escaped URL.
    – fuxia
    Feb 6, 2017 at 9:38
  • You're right, it worked only because of permalink caching I think. Feb 6, 2017 at 15:33
0

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.

1
  • 1
    Keep in mind that none of those are available in your CLI and therefore useless for cron jobs.
    – kaiser
    Feb 26, 2014 at 11:38
0

add_query_args( null, null ) will create an array element with empty key ($qs[""] = null) although it won't affect the result.


According to add_query_arg() | Function | WordPress Developer Resources, the 2nd, 3rd parameters are optional and they can be omitted.

add_query_args( null, null ) can be more shorter.

$current_url = add_query_args( [] );

There is also the shortest version although it isn't recommended as the 1st parameter is the required parameter.

$current_url = add_query_args();

In addition, note that both home_url( add_query_vars( [] ) ) and home_url( add_query_arg( null, null ) ) might not return actual URL when WordPress is installed in a sub-directory.

e.g. https://example.com/wp/wp/foo might be returned when WordPress is installed in https://example.com/wp/.


Update: 2017/01/23

My version based on the David's solution to get absolute URL.

$parts = parse_url(home_url());
$uri   = $parts['scheme'] . '://' . $parts['host'];

if (array_key_exists('port', $parts)) {
    $uri .= ':' . $parts['port'];
}

$uri .= add_query_arg([]);
0
-1

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

2
  • 1
    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. Feb 1, 2013 at 10:04
  • 2
    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. Jul 4, 2013 at 8:22
-1

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.

1
  • This returns a totally different URL other than the current page when I tried using it.
    – Kirby
    May 19, 2015 at 16:10
-2

You can use wordpress function to get current page URL

the_permalink()

This will return you the curremt page URL link.

1
  • 4
    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. Feb 26, 2014 at 11:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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