4

I am using this to allow users to delete their own posts on the front end of my site:

<a onclick="return confirm('Move this post to the trash? You can restore it later.');" href="<?php echo get_delete_post_link($postid); ?>">Trash Post</a>

The problem is, this refreshed the current page and adds some query args to the URL (trashed=1 and ids=123). What I want to happen is that the user is redirected to a certain page with specific query args, like so:

mysite.com/yourarticles/?user=123&post=321&action=trash

How can I change where the get_delete_post_link function redirects to?

4

To redirect after the use of get_delete_post_link() it's probably easiest to hook into the trashed_post action:

Code:

add_action( 'trashed_post', 'wpse132196_redirect_after_trashing', 10 );
function wpse132196_redirect_after_trashing() {
    wp_redirect( home_url('/your-custom-slug') );
    exit;
}

Or you could make it dependent on the according $_GET variable by hooking into the the action parse_request:

Code:

add_action( 'parse_request', 'wpse132196_redirect_after_trashing_get' );
function wpse132196_redirect_after_trashing_get() {
    if ( array_key_exists( 'trashed', $_GET ) && $_GET['trashed'] == '1' ) {
        wp_redirect( home_url('/your-custom-slug') );
        exit;
    }
}

Note that both solution will intercept on the admin side too, so you might want to add a check to prevent that.

To change the link returned by get_delete_post_link() take a look at the source, in link-template.php. There you'll see how the $delete_link is constructed. You can alter the return of the function via the correspondent filter get_delete_post_link. This way you can make the link point to your custom page or endpoint for frontend post deletion.

Code:

add_filter( 'get_delete_post_link', 'wpse132196_change_delete_post_link', 10, 3 );
function wpse132196_change_delete_post_link(  $id = 0, $deprecated = '', $force_delete = false ) {
    global $post;
    $action = ( $force_delete || !EMPTY_TRASH_DAYS ) ? 'delete' : 'trash';
    $qargs = array(
        'action' => $action,
        'post' => $post->ID,
        'user' => get_current_user_id()
    );
    $delete_link = add_query_arg( $qargs, home_url( sprintf( '/yourarcticles/' ) ) );
    return  wp_nonce_url( $delete_link, "$action-post_{$post->ID}" );
}

From where you can take care of handling your custom post deletion request. Note that above exemplary code won't delete anything, if I'm not mistaken, I haven't actually tested it, it's only proof of concept code, so you have to adapt to your needs yourself.

8
  • My pleasure, I hope you get it going with this. @Eckstein
    – Nicolai
    Feb 3 '14 at 20:13
  • Strangely the first example doesn't work with the check for if (!is_admin()). Oct 26 '17 at 7:41
  • @certainlyakey You are aware that is_admin() pretty much checks for being at the backend, administrative interface? So ! is_admin() means we are viewing the theme. If you want to distinguish users, like administrators, use current_user_can().
    – Nicolai
    Oct 26 '17 at 7:53
  • 1
    Yes, I am aware. I meant exactly that — if you want to "allow users to delete their own posts on the front end" you don't want the redirect to happen in the admin. But with that check in place redirect doesn't work. The second example works nicely though (with few corrections of purely spelling nature). Oct 26 '17 at 12:51
  • @certainlyakey I see, I'm not 100% sure at the moment what the reason would be. Next time edit the answer to fix errors, it is allowed, I would even say encouraged, personally I appreciate it.
    – Nicolai
    Oct 26 '17 at 12:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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