函数文档

_publish_post_hook()

💡 云策文档标注

概述

_publish_post_hook() 是一个 WordPress 钩子函数,用于在文章发布时调度 ping 和 enclosures。它依赖于 XMLRPC_REQUEST 和 WP_IMPORTING 常量来控制行为。

关键要点

  • 函数在文章发布时触发,主要用于处理 pingback、trackback 和 enclosures 的调度。
  • 使用 XMLRPC_REQUEST 常量判断是否为 XML-RPC 请求,如果是则触发 xmlrpc_publish_post 动作钩子。
  • 如果定义了 WP_IMPORTING 常量,函数会直接返回,不执行后续操作。
  • 根据 default_pingback_flag 选项和 get_to_ping() 结果,添加相应的 post meta 数据。
  • 调度 do_pings 事件,确保 ping 任务在适当时间运行。

代码示例

function _publish_post_hook( $post_id ) {
    if ( defined( 'XMLRPC_REQUEST' ) ) {
        do_action( 'xmlrpc_publish_post', $post_id );
    }

    if ( defined( 'WP_IMPORTING' ) ) {
        return;
    }

    if ( get_option( 'default_pingback_flag' ) ) {
        add_post_meta( $post_id, '_pingme', '1', true );
    }
    add_post_meta( $post_id, '_encloseme', '1', true );

    $to_ping = get_to_ping( $post_id );
    if ( ! empty( $to_ping ) ) {
        add_post_meta( $post_id, '_trackbackme', '1' );
    }

    if ( ! wp_next_scheduled( 'do_pings' ) ) {
        wp_schedule_single_event( time(), 'do_pings' );
    }
}

注意事项

  • 函数内部使用了多个 WordPress 核心函数,如 get_option、add_post_meta、wp_next_scheduled 等,开发者应确保这些函数在调用时可用。
  • 在 XML-RPC 请求或导入过程中,函数的行为可能不同,需注意相关常量的设置。

📄 原文内容

Hook to schedule pings and enclosures when a post is published.

Description

Uses XMLRPC_REQUEST and WP_IMPORTING constants.

Parameters

$post_idintrequired
The ID of the post being published.

Source

function _publish_post_hook( $post_id ) {
	if ( defined( 'XMLRPC_REQUEST' ) ) {
		/**
		 * Fires when _publish_post_hook() is called during an XML-RPC request.
		 *
		 * @since 2.1.0
		 *
		 * @param int $post_id Post ID.
		 */
		do_action( 'xmlrpc_publish_post', $post_id );
	}

	if ( defined( 'WP_IMPORTING' ) ) {
		return;
	}

	if ( get_option( 'default_pingback_flag' ) ) {
		add_post_meta( $post_id, '_pingme', '1', true );
	}
	add_post_meta( $post_id, '_encloseme', '1', true );

	$to_ping = get_to_ping( $post_id );
	if ( ! empty( $to_ping ) ) {
		add_post_meta( $post_id, '_trackbackme', '1' );
	}

	if ( ! wp_next_scheduled( 'do_pings' ) ) {
		wp_schedule_single_event( time(), 'do_pings' );
	}
}

Hooks

do_action( ‘xmlrpc_publish_post’, int $post_id )

Fires when _publish_post_hook() is called during an XML-RPC request.

Changelog

Version Description
2.3.0 Introduced.