wp_make_link_relative( string $link ): string

Converts full URL paths to absolute paths.


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.

Top ↑


$link string Required
Full URL path.

Top ↑


string Absolute path.

Top ↑


File: wp-includes/formatting.php. View all references

function wp_make_link_relative( $link ) {
	return preg_replace( '|^(https?:)?//[^/]+(/?.*)|i', '$2', $link );

Top ↑


Version Description
4.1.0 Support was added for relative URLs.
2.1.0 Introduced.

Top ↑

User Contributed Notes

  1. Skip to note 3 content
    Contributed by Jake Jackson

    Be aware that this function does not work as expected when a site uses a non-standard port.

    echo wp_make_link_relative( 'http://localhost:8080/wp_test/sample-page/' );

    will output


You must log in before being able to contribute a note or feedback.