Converts full URL paths to absolute paths.
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
stringrequired- Full URL path.
Source
function wp_make_link_relative( $link ) {
return preg_replace( '|^(https?:)?//[^/]+(/?.*)|i', '$2', $link );
}
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
/wp_test/sample-page/
.