Add frontend “Restore” link

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

Read More

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>

Related posts

Leave a Reply

1 comment

  1. 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 the untrash instruction is checking for a valid WordPress nonce, and not getting one.

    This might do the trick:

    <?php
    function wpse_95348_undelete_post( $post_id ) {
        // no post?
        if( !$post_id || !is_numeric( $post_id ) ) {
            return false;
        }
        $_wpnonce = wp_create_nonce( 'untrash-post_' . $post_id );
        $url = admin_url( 'post.php?post=' . $post_id . '&action=untrash&_wpnonce=' . $_wpnonce );
        return $url; 
    }
    ?>
    

    It uses wp_create_nonce() to generate the nonce you need and admin_url() to get a proper wp-admin URL.

    Further reading on WP nonces