函数文档

sanitize_trackback_urls()

💡 云策文档标注

概述

sanitize_trackback_urls() 函数用于清理以空格或回车分隔的 trackback URL 列表,确保它们以 http 或 https 协议开头,并经过 sanitize_url() 处理。

关键要点

  • 参数 $to_ping 是必需的字符串,包含以空格或回车分隔的 URL。
  • 返回一个字符串,其中 URL 以 http 或 https 开头,并用回车分隔。
  • 使用正则表达式分割输入,过滤掉非 http/https 协议的 URL,并应用 sanitize_url() 清理。
  • 提供 'sanitize_trackback_urls' 过滤器钩子,允许在清理后修改 URL 列表。

代码示例

function sanitize_trackback_urls( $to_ping ) {
    $urls_to_ping = preg_split( '/[rnt ]/', trim( $to_ping ), -1, PREG_SPLIT_NO_EMPTY );
    foreach ( $urls_to_ping as $k => $url ) {
        if ( ! preg_match( '#^https?://.#i', $url ) ) {
            unset( $urls_to_ping[ $k ] );
        }
    }
    $urls_to_ping = array_map( 'sanitize_url', $urls_to_ping );
    $urls_to_ping = implode( "n", $urls_to_ping );
    return apply_filters( 'sanitize_trackback_urls', $urls_to_ping, $to_ping );
}

注意事项

  • 函数在 WordPress 3.4.0 版本中引入。
  • 相关函数包括 get_to_ping() 和 wp_insert_post(),用于获取和插入需要 ping 的 URL。
  • Hook 为 apply_filters('sanitize_trackback_urls', $urls_to_ping, $to_ping),用于过滤清理后的 URL 列表。

📄 原文内容

Sanitizes space or carriage return separated URLs that are used to send trackbacks.

Parameters

$to_pingstringrequired
Space or carriage return separated URLs

Return

string URLs starting with the http or https protocol, separated by a carriage return.

Source

function sanitize_trackback_urls( $to_ping ) {
	$urls_to_ping = preg_split( '/[rnt ]/', trim( $to_ping ), -1, PREG_SPLIT_NO_EMPTY );
	foreach ( $urls_to_ping as $k => $url ) {
		if ( ! preg_match( '#^https?://.#i', $url ) ) {
			unset( $urls_to_ping[ $k ] );
		}
	}
	$urls_to_ping = array_map( 'sanitize_url', $urls_to_ping );
	$urls_to_ping = implode( "n", $urls_to_ping );
	/**
	 * Filters a list of trackback URLs following sanitization.
	 *
	 * The string returned here consists of a space or carriage return-delimited list
	 * of trackback URLs.
	 *
	 * @since 3.4.0
	 *
	 * @param string $urls_to_ping Sanitized space or carriage return separated URLs.
	 * @param string $to_ping      Space or carriage return separated URLs before sanitization.
	 */
	return apply_filters( 'sanitize_trackback_urls', $urls_to_ping, $to_ping );
}

Hooks

apply_filters( ‘sanitize_trackback_urls’, string $urls_to_ping, string $to_ping )

Filters a list of trackback URLs following sanitization.

Changelog

Version Description
3.4.0 Introduced.