I am using get_delete_posts_link($postid)
to let users send their posts to the trash from the frontend. Now I’m trying to replicate the same functionality for them to “untrash” or restore the posts as well.
get_delete_posts_link($postid)
seems to generate a URL like this: http://mysite.com/wordpress/wp-admin/post.php?post=555&action=trash&_wpnonce=0d6fc30f3b
And when I go to wp-admin and look at what link the Restore link uses, it looks to be same EXCEPT that is uses action=untrash instead of action=trash.
Since there doesn’t seem to be a function the mirrors the delete post link function, I thought that perhaps manually creating the URL with variables and using the “untrash” action would do it, but it doesn’t seem to work that way, restoring won’t work.
What’s the best way to put a link to “restore” a post on the frontend?
EDIT: This is the markup I am using the create the “restore” link (not working):
<?php
$untrashurl = get_bloginfo('wpurl') . '/wp-admin/post.php?post=' . $postid . '&action=untrash';
?>
<a class="db-post-link" href="<?php echo wp_nonce_url($untrashurl); ?>">Untrash</a>
From your comment above, I believe you’re running into issues with the
_wpnonce
piece of the puzzle. Looking at the code in/wp-admin/post.php
, it appears that theuntrash
instruction is checking for a valid WordPress nonce, and not getting one.This might do the trick:
It uses
wp_create_nonce()
to generate the nonce you need andadmin_url()
to get a properwp-admin
URL.Further reading on WP nonces