Get the current page URL (including pagination)

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.)

Read More

I know about this function already, but I would be glad if there was a “native” WP function that does the job.

Related posts

Leave a Reply

8 comments

  1. 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.

  2. 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.

  3. 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([]);
    
  4. 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