links_add_base_url() WordPress Function

The links_add_base_url() function in WordPress allows you to add a base URL to relative links in your content. This can be useful if you move your WordPress site to a new domain or subdomain, as it ensures that all links in your content will continue to work.

links_add_base_url( string $content, string $base, array $attrs = array('src', 'href') ) #

Adds a base URL to relative links in passed content.


Description

By default it supports the ‘src’ and ‘href’ attributes. However this can be changed via the 3rd param.


Top ↑

Parameters

$content

(string)(Required)String to search for links in.

$base

(string)(Required)The base URL to prefix to links.

$attrs

(array)(Optional)The attributes which should be processed.

Default value: array('src', 'href')


Top ↑

Return

(string) The processed content.


Top ↑

Source

File: wp-includes/formatting.php

function links_add_base_url( $content, $base, $attrs = array( 'src', 'href' ) ) {
	global $_links_add_base;
	$_links_add_base = $base;
	$attrs           = implode( '|', (array) $attrs );
	return preg_replace_callback( "!($attrs)=(['\"])(.+?)\\2!i", '_links_add_base', $content );
}


Top ↑

Changelog

Changelog
VersionDescription
2.7.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More