函数文档

get_to_ping()

💡 云策文档标注

概述

get_to_ping() 函数用于检索指定文章中待 ping 的 URL 列表。它接受文章 ID 或 WP_Post 对象作为参数,返回一个 URL 数组或 false。

关键要点

  • 参数:$post(必需),可以是整数(文章 ID)或 WP_Post 对象。
  • 返回值:成功时返回待 ping 的 URL 字符串数组,失败时返回 false。
  • 内部处理:通过 sanitize_trackback_urls() 清理 URL,并使用正则表达式分割字符串。
  • 过滤器:提供 'get_to_ping' 过滤器,允许开发者修改待 ping 的 URL 列表。
  • 相关函数:包括 sanitize_trackback_urls()、apply_filters() 和 get_post()。
  • 变更历史:从 WordPress 4.7.0 起支持 WP_Post 对象参数,最初在 1.5.0 版本引入。

代码示例

function get_to_ping( $post ) {
    $post = get_post( $post );

    if ( ! $post ) {
        return false;
    }

    $to_ping = sanitize_trackback_urls( $post->to_ping );
    $to_ping = preg_split( '/s/', $to_ping, -1, PREG_SPLIT_NO_EMPTY );

    /**
     * Filters the list of URLs yet to ping for the given post.
     *
     * @since 2.0.0
     *
     * @param string[] $to_ping List of URLs yet to ping.
     */
    return apply_filters( 'get_to_ping', $to_ping );
}

注意事项

  • 确保传入有效的文章 ID 或 WP_Post 对象,否则函数可能返回 false。
  • URL 列表通过空格或回车符分隔,并使用 sanitize_trackback_urls() 进行清理。
  • 可以利用 'get_to_ping' 过滤器自定义待 ping 的 URL 列表。

📄 原文内容

Retrieves URLs that need to be pinged.

Parameters

$postint|WP_Postrequired
Post ID or post object.

Return

string[]|false List of URLs yet to ping.

Source

function get_to_ping( $post ) {
	$post = get_post( $post );

	if ( ! $post ) {
		return false;
	}

	$to_ping = sanitize_trackback_urls( $post->to_ping );
	$to_ping = preg_split( '/s/', $to_ping, -1, PREG_SPLIT_NO_EMPTY );

	/**
	 * Filters the list of URLs yet to ping for the given post.
	 *
	 * @since 2.0.0
	 *
	 * @param string[] $to_ping List of URLs yet to ping.
	 */
	return apply_filters( 'get_to_ping', $to_ping );
}

Hooks

apply_filters( ‘get_to_ping’, string[] $to_ping )

Filters the list of URLs yet to ping for the given post.

Changelog

Version Description
4.7.0 $post can be a WP_Post object.
1.5.0 Introduced.