wp_get_post_revisions_url()
Gets the URL of the revisions page of a post, where you can view changes and, if needed, restore one of the revisions.
The function accepts the post ID or a WP_Post object. If the parameter is not passed, the current global post is used.
If the passed post is already a revision, a link to edit this revision will be returned. If the post is not found, revisions are disabled for it, or the post has no revisions yet, the function will return null.
Also important to consider that the URL is generated via get_edit_post_link(), so the result depends on the current user’s permissions to edit the post.
No Hooks.
Returns
String|null.
string— URL of the edit page of the post’s latest revision.null— if the link cannot be obtained.
Usage
wp_get_post_revisions_url( $post );
- $post(int|WP_Post)
- ID of the post or post object.
Default:0— current global post.
Examples
#1 Getting a link to the post revisions
We’ll get the revisions page URL for the post with ID 25.
$revisions_url = wp_get_post_revisions_url( 25 );
if ( $revisions_url ) {
echo '<a href="' . esc_url( $revisions_url ) . '">View Revisions</a>';
} #2 Getting a link to the revisions of the current post
If the code runs inside a WordPress loop, you do not need to pass the parameter.
$revisions_url = wp_get_post_revisions_url();
if ( null === $revisions_url ) {
return;
}
echo '<a href="' . esc_url( $revisions_url ) . '">Revision History</a>'; #3 Checking for a link before output
The function may return null, so before outputting the link, it’s better to always check the result.
$post_id = get_the_ID();
$revisions_url = wp_get_post_revisions_url( $post_id );
if ( ! $revisions_url ) {
echo 'There are no available revisions for this post.';
return;
}
echo '<a href="' . esc_url( $revisions_url ) . '">Open Revisions</a>';
Changelog
| Since 5.9.0 | Introduced. |