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
wp-includes/formatting.php
This should output the following URL:
wp-includes/formatting.php
Example with Post ID:
wp-includes/formatting.php
Where
$post
is your post object.Example for current post:
wp-includes/formatting.php
Be aware that this function does not work as expected when a site uses a non-standard port.
wp-includes/formatting.php
will output
wp-includes/formatting.php
/wp_test/sample-page/
.