函数文档

wp_get_post_revisions_url()

💡 云策文档标注

概述

wp_get_post_revisions_url() 函数用于获取指定文章的修订版本查看和恢复 URL。它基于文章 ID 或 WP_Post 对象,返回编辑修订的链接或 null。

关键要点

  • 参数 $post 可选,可以是整数(文章 ID)或 WP_Post 对象,默认为全局 $post。
  • 返回值为字符串(修订编辑 URL)或 null(如果文章无效、修订未启用或无修订)。
  • 函数内部会检查文章类型、修订启用状态和修订数量,以决定是否返回 URL。
  • 如果文章是修订版本,直接返回其编辑链接;否则,返回最新修订的编辑链接。

代码示例

function wp_get_post_revisions_url( $post = 0 ) {
    $post = get_post( $post );

    if ( ! $post instanceof WP_Post ) {
        return null;
    }

    // If the post is a revision, return early.
    if ( 'revision' === $post->post_type ) {
        return get_edit_post_link( $post );
    }

    if ( ! wp_revisions_enabled( $post ) ) {
        return null;
    }

    $revisions = wp_get_latest_revision_id_and_total_count( $post->ID );

    if ( is_wp_error( $revisions ) || 0 === $revisions['count'] ) {
        return null;
    }

    return get_edit_post_link( $revisions['latest_id'] );
}

注意事项

  • 确保文章存在且修订功能已启用,否则函数可能返回 null。
  • 函数自 WordPress 5.9.0 版本引入,使用时需注意版本兼容性。
  • 相关函数包括 wp_get_latest_revision_id_and_total_count()、get_edit_post_link() 等,用于辅助处理修订逻辑。

📄 原文内容

Returns the url for viewing and potentially restoring revisions of a given post.

Parameters

$postint|WP_Postoptional
Post ID or WP_Post object. Default is global $post.

Return

string|null The URL for editing revisions on the given post, otherwise null.

Source

function wp_get_post_revisions_url( $post = 0 ) {
	$post = get_post( $post );

	if ( ! $post instanceof WP_Post ) {
		return null;
	}

	// If the post is a revision, return early.
	if ( 'revision' === $post->post_type ) {
		return get_edit_post_link( $post );
	}

	if ( ! wp_revisions_enabled( $post ) ) {
		return null;
	}

	$revisions = wp_get_latest_revision_id_and_total_count( $post->ID );

	if ( is_wp_error( $revisions ) || 0 === $revisions['count'] ) {
		return null;
	}

	return get_edit_post_link( $revisions['latest_id'] );
}

Changelog

Version Description
5.9.0 Introduced.