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.
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:The function falls back on
$_SERVER[ 'REQUEST_URI' ]
and appliesurlencode_deep()
to it. See https://github.com/WordPress/WordPress/blob/3.8/wp-includes/functions.php#L673Edit:
As
$_SERVER[ 'REQUEST_URI' ]
represents unfiltered user input, one should always escape the return value ofadd_query_arg()
when the context is changed. For example, useesc_url_raw()
for DB usage oresc_attr()
oresc_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:WordPress core does not support port, user or password in a multisite site URL so this should be sufficient.
Not a function, but definately using wordpress code..
http://kovshenin.com/2012/current-url-in-wordpress/
For me
<?php esc_url(the_permalink()); ?>
works (on a archive page with pagination).1)
$_SERVER['REQUEST_URI']
– It return the URL in to access the page which is executing the script. If you need to typehttp://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 andD:/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 likeexample.com
when the you findhttp://example.com
in browserâs address-bar and returnwww.example.com
when you seehttp://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 forhttp://example.com
is not same as for thehttp://www.example.com
.4)
$_SERVER['HTTP_USER_AGENT']
– Returns the user agentâs (browser) detail accessing the web page. We can usestrpos($_SERVER["HTTP_USER_AGENT"],âMSIEâ)
to detect Microsoft Internet explorer or you can usestrpos($_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 URLhttp://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 URLhttp://www.example.com/index.php?id=5&page=product
then it returnsid=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 likevar/example.com/www/product.php
in Linux and path likeD:/xampp/xampp/htdocs/test/example.php
in windows.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.There is also the shortest version although it isn’t recommended as the 1st parameter is the required parameter.
In addition, note that both
home_url( add_query_vars( [] ) )
andhome_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 inhttps://example.com/wp/
.Update: 2017/01/23
My version based on the David’s solution to get absolute URL.
I dont now of pagination
but
You can use this function to get url within the loop
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)
or if u prefer to use php function you need to get the id outside the loop
wp_guess_url is what you are looking for.
You can use wordpress function to get current page URL
This will return you the curremt page URL link.