函数文档

weblog_ping()

💡 云策文档标注

概述

weblog_ping() 函数用于发送 pingback 通知,以告知其他博客本站已更新。它通过 XML-RPC 协议与目标服务器通信,支持扩展 ping 和普通 ping 两种方式。

关键要点

  • 函数接受两个必需参数:$server(目标博客主机)和 $path(发送 ping 的路径)。
  • 使用 WP_HTTP_IXR_Client 类进行 XML-RPC 通信,设置超时为 3 秒并自定义用户代理。
  • 首先尝试 weblogUpdates.extendedPing 方法,失败后回退到 weblogUpdates.ping 方法。
  • 相关函数包括 trailingslashit()、get_bloginfo()、home_url() 和 get_option()。

代码示例

function weblog_ping( $server = '', $path = '' ) {
	require_once ABSPATH . WPINC . '/class-IXR.php';
	require_once ABSPATH . WPINC . '/class-wp-http-ixr-client.php';

	// Using a timeout of 3 seconds should be enough to cover slow servers.
	$client             = new WP_HTTP_IXR_Client( $server, ( ( ! strlen( trim( $path ) ) || ( '/' === $path ) ) ? false : $path ) );
	$client->timeout    = 3;
	$client->useragent .= ' -- WordPress/' . get_bloginfo( 'version' );

	// When set to true, this outputs debug messages by itself.
	$client->debug = false;
	$home          = trailingslashit( home_url() );
	if ( ! $client->query( 'weblogUpdates.extendedPing', get_option( 'blogname' ), $home, get_bloginfo( 'rss2_url' ) ) ) { // Then try a normal ping.
		$client->query( 'weblogUpdates.ping', get_option( 'blogname' ), $home );
	}
}

📄 原文内容

Sends a pingback.

Parameters

$serverstringrequired
Host of blog to connect to.
$pathstringrequired
Path to send the ping.

Source

function weblog_ping( $server = '', $path = '' ) {
	require_once ABSPATH . WPINC . '/class-IXR.php';
	require_once ABSPATH . WPINC . '/class-wp-http-ixr-client.php';

	// Using a timeout of 3 seconds should be enough to cover slow servers.
	$client             = new WP_HTTP_IXR_Client( $server, ( ( ! strlen( trim( $path ) ) || ( '/' === $path ) ) ? false : $path ) );
	$client->timeout    = 3;
	$client->useragent .= ' -- WordPress/' . get_bloginfo( 'version' );

	// When set to true, this outputs debug messages by itself.
	$client->debug = false;
	$home          = trailingslashit( home_url() );
	if ( ! $client->query( 'weblogUpdates.extendedPing', get_option( 'blogname' ), $home, get_bloginfo( 'rss2_url' ) ) ) { // Then try a normal ping.
		$client->query( 'weblogUpdates.ping', get_option( 'blogname' ), $home );
	}
}

Changelog

Version Description
1.2.0 Introduced.