函数文档

do_trackbacks()

💡 云策文档标注

概述

do_trackbacks() 函数用于执行文章的 trackback 操作,处理待 ping 的 URL 列表。它接受一个文章 ID 或 WP_Post 对象作为参数,在失败时返回 false。

关键要点

  • 参数 $post 为必需,可以是文章 ID 或 WP_Post 对象,用于指定要执行 trackback 的文章。
  • 函数返回 void 或 false,失败时返回 false。
  • 内部逻辑包括获取待 ping 和已 ping 的 URL 列表,处理文章摘要和标题,并循环调用 trackback() 函数发送 trackback。
  • 使用多个 Hook,如 apply_filters('the_content'), apply_filters('the_excerpt'), apply_filters('the_title') 来过滤内容。
  • 相关函数包括 get_to_ping(), get_pung(), trackback(), wp_html_excerpt() 等。

代码示例

function do_trackbacks( $post ) {
    global $wpdb;

    $post = get_post( $post );

    if ( ! $post ) {
        return false;
    }

    $to_ping = get_to_ping( $post );
    $pinged  = get_pung( $post );

    if ( empty( $to_ping ) ) {
        $wpdb->update( $wpdb->posts, array( 'to_ping' => '' ), array( 'ID' => $post->ID ) );
        return;
    }

    if ( empty( $post->post_excerpt ) ) {
        /** This filter is documented in wp-includes/post-template.php */
        $excerpt = apply_filters( 'the_content', $post->post_content, $post->ID );
    } else {
        /** This filter is documented in wp-includes/post-template.php */
        $excerpt = apply_filters( 'the_excerpt', $post->post_excerpt );
    }

    $excerpt = str_replace( ']]>', ']]>', $excerpt );
    $excerpt = wp_html_excerpt( $excerpt, 252, '…' );

    /** This filter is documented in wp-includes/post-template.php */
    $post_title = apply_filters( 'the_title', $post->post_title, $post->ID );
    $post_title = strip_tags( $post_title );

    foreach ( (array) $to_ping as $tb_ping ) {
        $tb_ping = trim( $tb_ping );
        if ( ! in_array( $tb_ping, $pinged, true ) ) {
            trackback( $tb_ping, $post_title, $excerpt, $post->ID );
            $pinged[] = $tb_ping;
        } else {
            $wpdb->query(
                $wpdb->prepare(
                    "UPDATE $wpdb->posts SET to_ping = TRIM(REPLACE(to_ping, %s, '')) WHERE ID = %d",
                    $tb_ping,
                    $post->ID
                )
            );
        }
    }
}

注意事项

  • 从版本 4.7.0 开始,$post 参数支持 WP_Post 对象。
  • 函数内部使用数据库操作更新文章状态,需确保数据库连接正常。
  • trackback 功能依赖于外部 URL 的可访问性,发送失败可能影响函数执行。

📄 原文内容

Performs trackbacks.

Parameters

$postint|WP_Postrequired
Post ID or object to do trackbacks on.

Return

void|false Returns false on failure.

Source

function do_trackbacks( $post ) {
	global $wpdb;

	$post = get_post( $post );

	if ( ! $post ) {
		return false;
	}

	$to_ping = get_to_ping( $post );
	$pinged  = get_pung( $post );

	if ( empty( $to_ping ) ) {
		$wpdb->update( $wpdb->posts, array( 'to_ping' => '' ), array( 'ID' => $post->ID ) );
		return;
	}

	if ( empty( $post->post_excerpt ) ) {
		/** This filter is documented in wp-includes/post-template.php */
		$excerpt = apply_filters( 'the_content', $post->post_content, $post->ID );
	} else {
		/** This filter is documented in wp-includes/post-template.php */
		$excerpt = apply_filters( 'the_excerpt', $post->post_excerpt );
	}

	$excerpt = str_replace( ']]>', ']]>', $excerpt );
	$excerpt = wp_html_excerpt( $excerpt, 252, '…' );

	/** This filter is documented in wp-includes/post-template.php */
	$post_title = apply_filters( 'the_title', $post->post_title, $post->ID );
	$post_title = strip_tags( $post_title );

	foreach ( (array) $to_ping as $tb_ping ) {
		$tb_ping = trim( $tb_ping );
		if ( ! in_array( $tb_ping, $pinged, true ) ) {
			trackback( $tb_ping, $post_title, $excerpt, $post->ID );
			$pinged[] = $tb_ping;
		} else {
			$wpdb->query(
				$wpdb->prepare(
					"UPDATE $wpdb->posts SET to_ping = TRIM(REPLACE(to_ping, %s, '')) WHERE ID = %d",
					$tb_ping,
					$post->ID
				)
			);
		}
	}
}

Hooks

apply_filters( ‘the_content’, string $content )

Filters the post content.

apply_filters( ‘the_excerpt’, string $post_excerpt )

Filters the displayed post excerpt.

apply_filters( ‘the_title’, string $post_title, int $post_id )

Filters the post title.

Changelog

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