Adding base URL to relative links with links_add_base_url in WordPress

The WordPress links_add_base_url function is a part of the WordPress API. This function is primarily used to add the base URL to relative links in a given content. The base URL is the part of the URL that comes before any specific page, post, or other kind of content on a website.

When a website’s content contains relative links, these links may not work correctly if the content is viewed outside of the original website context. This is where the links_add_base_url function comes into play. It processes the content and ensures that all relative links have the correct base URL, thus preventing broken links when the content is shared or viewed in different contexts.

It should be noted that the links_add_base_url function only affects relative links. Absolute links, which already include the base URL, are not modified by this function.

Parameters

The links_add_base_url function in WordPress accepts the following parameters:

  • $content (string), required. Description: The string to search for links in.
  • $base (string), required. Description: The base URL to prefix to links.
  • $attrs (array), optional. Default value: array('src', 'href'). Description: The attributes that should be processed.

Return Value

The links_add_base_url function returns the following:

string: The processed content.

Examples

How to Add Base URL to All Links in Content

This code snippet demonstrates how to use the links_add_base_url function to add a base URL to all href and src attributes in a given content string.

$content = 'Check out this link: <a href="/page">Click Here</a> and this image: <img src="/image.jpg" />';
$base_url = 'https://example.com';

$processed_content = links_add_base_url($content, $base_url);

echo $processed_content;

This code snippet takes a content string containing a link and an image with relative URLs. It uses the links_add_base_url function to add the base URL https://example.com to the href and src attributes, converting them to absolute URLs.

How to Add Base URL to Only href Attributes

This code snippet shows how to use the links_add_base_url function to add a base URL only to href attributes in a given content string.

$content = 'Visit our site: <a href="/about">About Us</a>';
$base_url = 'https://example.com';
$attrs = array('href');

$processed_content = links_add_base_url($content, $base_url, $attrs);

echo $processed_content;

This code snippet takes a content string containing a link with a relative URL. It uses the links_add_base_url function to add the base URL https://example.com only to the href attribute, converting it to an absolute URL.

How to Add Base URL to Custom Attributes

This code snippet demonstrates how to use the links_add_base_url function to add a base URL to custom attributes in a given content string.

$content = 'Check this out: <a data-custom="/custom-link">Custom Link</a>';
$base_url = 'https://example.com';
$attrs = array('data-custom');

$processed_content = links_add_base_url($content, $base_url, $attrs);

echo $processed_content;

This code snippet takes a content string containing a link with a custom attribute data-custom that has a relative URL. It uses the links_add_base_url function to add the base URL https://example.com to the data-custom attribute, converting it to an absolute URL.

Conclusion

The links_add_base_url function in WordPress is used to add the base URL to relative links in a given content. This function is particularly useful when you want to ensure that all the links in your content are absolute rather than relative. By using the links_add_base_url function, you can programmatically ensure that all the links in your content will work correctly, regardless of the context in which the content is displayed.

Related WordPress Functions