Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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>
share|improve this question
    
What's failing? Is it related to the _wpnonce field? –  Pat J Apr 10 '13 at 21:12
    
When I click the link it goes to a page that says "Are you sure you want to do this?" with a link that says "try again" and then when I click "try again" it takes me back to the previous page and the post status is unchanged. –  Eckstein Apr 10 '13 at 21:53
    
This is definitely a nonce error –  vancoder Apr 10 '13 at 22:41
    
trashed posts shouldnt be visible on the frontend, therefore you must have some code that lists them, can you put this in your question? –  Tom J Nowell Apr 10 '13 at 22:55
    
The answer below worked, but to answer your questions real quick, I am simply using a custom query to call posts with all posts statuses (including publish, draft, trash, etc). –  Eckstein Apr 10 '13 at 23:11
add comment

1 Answer

up vote 3 down vote accepted

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

share|improve this answer
    
Excellent this worked! Just wondering why wp_nonce_url() doesn't work the same way? I will read up on nonce (I'm not too familiar with it) to get that part of my skills up. Thanks for the answer! –  Eckstein Apr 10 '13 at 23:11
    
Reading the Codex page for wp_nonce_url(), it looks like you should be able to use wp_nonce_url( admin_url( 'post.php?post=' . $post_id . '&action=untrash' ), 'untrash-post_' . $post_id ). I'm still fairly new to nonces, so I hadn't noticed the wp_nonce_url() function before. –  Pat J Apr 10 '13 at 23:33
    
Oh, it was probably the admin_url thing that was actually wrong I bet. Anyway, I like the function better anyway. Much cleaner! –  Eckstein Apr 10 '13 at 23:46
    
Looking again at the call to wp_nonce_url() in your question, I think I see the problem. You're not sending anything for the 2nd argument -- $action -- and so WP isn't able to associate the nonce you're creating with the untrash-post_{ID} action. –  Pat J Apr 10 '13 at 23:47
    
Ooooh, OK that sort of makes sense. It says that action is optional, but perhaps even being optional in needs the context in order to actually do anything. –  Eckstein Apr 11 '13 at 0:16
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.