get_delete_post_link redirect

The question:

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); ?>" rel="nofollow noreferrer noopener">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?

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

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.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment