Using wp_make_link_relative to convert absolute URLs to relative in WordPress

The WordPress function wp_make_link_relative is a useful tool that converts absolute URLs to relative URLs. In other words, it removes the domain and protocol (http://, https://, etc.) from the URL, leaving only the path and query string if present.

This function can be particularly helpful in various situations. For instance, when you’re moving a WordPress site from one domain to another, you might encounter issues with absolute URLs pointing to the old domain. By converting these to relative URLs, you can avoid such problems. It’s also useful when you want to make your HTML more concise, or when you want to ensure that your site’s links will work regardless of the domain or protocol.

The wp_make_link_relative function is an essential component for managing URLs within a WordPress site, offering a simple and effective way to handle domain and protocol changes, improve the compactness of your HTML, and enhance the robustness of your site’s links.

Understanding the Parameters of the wp_make_link_relative Function in WordPress

The wp_make_link_relative function in WordPress is a handy tool that accepts a specific set of parameters. This function is primarily designed to process a full URL path and return it as a relative path. Here, we will discuss the parameters it accepts and the value it returns.

Parameters Accepted by the wp_make_link_relative Function

  • $link (string): This is a required parameter that represents the full URL path that you want to process. It’s the URL you wish to convert from an absolute path to a relative one.

Value Returned by the wp_make_link_relative Function

The wp_make_link_relative function processes the given full URL path and returns it as a relative path. In technical terms, the return value of this function is a string representing the absolute path.


How to Make a Link Relative in WordPress

One of the most common usages of the wp_make_link_relative function is to make a URL relative. This can be useful when you want to manipulate the URL for various purposes, such as for SEO or for creating cleaner URLs.

$url = "";
$relative_url = wp_make_link_relative($url);
echo $relative_url;

In the above code, we first define a variable $url and assign it an absolute URL. Then we use the wp_make_link_relative function to convert this absolute URL into a relative URL and assign it to the $relative_url variable. Finally, we echo out the $relative_url to display it.

How to Use wp_make_link_relative in a Loop

The wp_make_link_relative function can also be used in a loop to convert multiple absolute URLs into relative URLs.

$urls = array("", "", "");
foreach ($urls as $url) {
 $relative_url = wp_make_link_relative($url);
 echo $relative_url . "<br />";

In this code, we first define an array $urls that contains multiple absolute URLs. We then use a foreach loop to iterate over each URL in the array. For each URL, we use the wp_make_link_relative function to convert it into a relative URL and then echo it out.


The wp_make_link_relative function is a WordPress function that serves to convert an absolute URL into a relative one. This function is particularly useful in WordPress development when the need arises to move a site from one domain to another, as it aids in maintaining the integrity of links within the site. By converting links to a relative format, the wp_make_link_relative function ensures that these links remain valid and functional, regardless of the domain on which the site is currently hosted.

Related WordPress Functions