Wordpress - Get the current page URL (including pagination)
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.
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/