How to get (or generate) link to untrash a post?

I have a custom admin page on a WordPress that allows showing the posts in a certain required way.

Each post is represented as a card that includes a trash icon to send the post to the trash. It’s easy with:

<?php $trash_link = get_delete_post_link( $id ); ?>
<a href="<?= $trash_link ?>"><span class="dashicons dashicons-trash"></span></a>

I’d like to replace this icon/link for a restore one when I’m currently viewing my trashed posts but I cannot figure out how to get the link since it doesn’t seem to exist a get_undelete_post_link() or get_untrash_post_link().

Trash links look like:

http://example.com/wp-admin/post.php?post=8459&action=trash&_wpnonce=937a7ccb5d

While untrash links look like:

http://example.com/wp-admin/post.php?post=8459&action=untrash&_wpnonce=53facd85e3

So I tried:

<?php
$trash_link = get_delete_post_link( $id );
if ( $post->post_status == 'trash' ):
    $restore_link = str_replace( 'trash', 'untrash', $trash_link );
endif;

That gives an apparently correct link but, of course, it doesn’t work since nonce doesn’t verify.

I guess I could build my own system using wp_create_nonce() and wp_untrash_post(), but is there any easier way?

Read more here: How to get (or generate) link to untrash a post?

Leave a Reply

Your email address will not be published. Required fields are marked *