函数文档

links_add_base_url()

💡 云策文档标注

概述

links_add_base_url() 是一个 WordPress 函数,用于在内容中的相对链接前添加基础 URL。它默认处理 'src' 和 'href' 属性,但可通过参数自定义。

关键要点

  • 函数功能:为传入内容中的相对链接添加基础 URL,支持自定义处理的属性。
  • 参数说明:$content(必需,要搜索链接的字符串)、$base(必需,要添加的基础 URL)、$attrs(可选,要处理的属性数组,默认为 array('src', 'href'))。
  • 返回值:返回处理后的字符串内容。

代码示例

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 );
}

注意事项

  • 该函数从 WordPress 2.7.0 版本引入。
  • 相关函数:install_plugin_information() 在插件安装信息显示中使用此函数。

📄 原文内容

Adds a base URL to relative links in passed content.

Description

By default, this function supports the ‘src’ and ‘href’ attributes.
However, this can be modified via the $attrs parameter.

Parameters

$contentstringrequired
String to search for links in.
$basestringrequired
The base URL to prefix to links.
$attrsstring[]optional
The attributes which should be processed.

Default:array(, )

Return

string The processed content.

Source

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 );
}

Changelog

Version Description
2.7.0 Introduced.