wp_make_link_relative( string $link ): string
Converts full URL paths to absolute paths.
Contents
Description
Removes the http or https protocols and the domain. Keeps the path ‘/’ at the beginning, so it isn’t a true relative link, but from the web root base.
Parameters
-
$link
string Required -
Full URL path.
Return
string Absolute path.
Source
File: wp-includes/formatting.php
.
View all references
function wp_make_link_relative( $link ) {
return preg_replace( '|^(https?:)?//[^/]+(/?.*)|i', '$2', $link );
}
Changelog
Version | Description |
---|---|
4.1.0 | Support was added for relative URLs. |
2.1.0 | Introduced. |
User Contributed Notes
You must log in before being able to contribute a note or feedback.
Basic Example
This should output the following URL:
Example with Post ID:
Where
$post
is your post object.Example for current post:
Be aware that this function does not work as expected when a site uses a non-standard port.
will output
Top ↑
Feedback
In my test the output was
/wp_test/sample-page/
. — By Daniel Kossmann —