get_self_link()
Gets the URL (link) of the current page (request) that triggered the currently running code.
For example, it will return the URL https://example.com, if we call this function on the homepage of the site.
See also: self_link().
1 time — 0.0135138 sec (extremely slow) | 50000 times — 0.80 sec (very fast)
No Hooks.
Returns
String. The correct link for the element atom:self.
Usage
get_self_link();
Examples
#1 Demo of what the function outputs on different pages
In short, the function always outputs exactly what is currently in the browser line. If it is not a browser request, it will output the URL to which the request was made.
echo get_self_link(); // Home: http://wptest.loc/ // Post page: http://wptest.loc/template-sticky/ // Admin (home): http://wptest.loc/wp-admin/ // Category (with custom parameters): http://wptest.loc/category/uncategorized/?foo=bar
Changelog
| Since 5.3.0 | Introduced. |
get_self_link() get self link code WP 6.9
function get_self_link() {
$parsed = parse_url( home_url() );
$domain = $parsed['host'];
if ( isset( $parsed['port'] ) ) {
$domain .= ':' . $parsed['port'];
}
return set_url_scheme( 'http://' . $domain . wp_unslash( $_SERVER['REQUEST_URI'] ) );
}