最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

permalinks - Get the current page URL (including pagination)

programmeradmin1浏览0评论

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.

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.

Share Improve this question edited Dec 19, 2020 at 13:18 René asked Jan 31, 2013 at 22:05 RenéRené 3651 gold badge5 silver badges12 bronze badges
Add a comment  | 

8 Answers 8

Reset to default 37

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/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/2012/current-url-in-wordpress/

add_query_args( null, null ) will create an array element with empty key ($qs[""] = null) although it won't affect the result.

  • functions.php in tags/4.6/src/wp-includes – WordPress Trac

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/wp/wp/foo might be returned when WordPress is installed in https://example/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([]);

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 type http://www.example/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 when the you find http://example in browser’s address-bar and return www.example when you see http://www.example 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 is not same as for the http://www.example.

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/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/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/www/product.php in Linux and path like D:/xampp/xampp/htdocs/test/example.php in windows.

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

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.

You can use wordpress function to get current page URL

the_permalink()

This will return you the curremt page URL link.

发布评论

评论列表(0)

  1. 暂无评论