Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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
add comment

6 Answers

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
add comment

You can use wordpress function to get current page URL

the_permalink()

This will return you the curremt page URL link.

share|improve this answer
1  
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 at 11:10
add comment

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 at 11:38
add comment

In addition to Rajeev Vyas answer, you needn't to pass any non-empty parameter to add_query_arg(). The following worked allways 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 ) );

It's really not a very talking line of code, but it works. In this case, the function falls back on $_SERVER[ 'REQUEST_URI' ] and applies urlencode_deep() to it: https://github.com/WordPress/WordPress/blob/3.8/wp-includes/functions.php#L673

share|improve this answer
add comment
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/

share|improve this answer
    
I'll give this a try, thanks. –  René Schubert Feb 1 '13 at 10:01
1  
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
add comment

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
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. –  René Schubert Feb 1 '13 at 10:04
1  
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. –  jleander Jul 4 '13 at 8:22
add comment

Your Answer

 
discard

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.