Can someone please tell me how to get the correct url of the page the user is on? I need to grab the url and construct a new link. Here is the issue that I am having. For example, let's say I am on http://localhost/myplugin/samplepage/?some_var=1&some_var=2

home_url() is http://localhost/myplugin
$_SERVER['REQUEST_URI] is /myplugin/samplepage/?some_var=1&some_var=2

As you can see, if I do this

$url = home_url() . $_SERVER['REQUEST_URI];

The outcome will be

http://localhost/myplugin/myplugin/samplepage/?some_var=1&some_var=2

Notice the 'myplugin' in the url twice, which doesn't work.

link|improve this question

feedback

1 Answer

up vote 1 down vote accepted

The built-in redirect_canonical() uses the following:

$requested_url  = is_ssl() ? 'https://' : 'http://';                                                                                                                                           
$requested_url .= $_SERVER['HTTP_HOST'];                                                                                                                                                       
$requested_url .= $_SERVER['REQUEST_URI'];
link|improve this answer
Thanks Adam. That did the trick. – pistolshrimp Jan 21 at 5:30
feedback

Your Answer

 
or
required, but never shown

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