Delete link on single-custom.php with redirection

I need to provide a delete link for my users on the single page of a custom post type. But when the entry is deleted the user will end in a nothing found on this page wordpress error (because we are still on the entry site + /?deleted=1). How can i redirect the user to another page on my site?

This is how I’m posting my delete link:

Read More
if ( !current_user_can( 'delete_bkroadkill', $post->ID ) )
            return;

        $onclick =  "onclick="return confirm('Rly delete?');"";
        $link = "<a " . $onclick. " href='" . wp_nonce_url( get_bloginfo('url') . "/wordpress/wp-admin/post.php?action=delete&amp;post=" . $post->ID, 'delete-post_' . $post->ID) . "'>" . 'Löschen' . "</a>";
        echo $before . $link . $after;

and I simply call this function in my single-bkroadkill.php

  <?php wp_delete_post_link(); ?>

I already tried by using this hook:

add_action('trashed_post','my_trashed_post_handler',10,1);
function my_trashed_post_handler($post_id) {
   if (!is_admin()) {
    wp_redirect('http://www.citizen-science.at/projekte/roadkill/ubersicht');
    exit;
   }
}

But it had no effect at all. Is this only working for standard posts? I couldn’t find any documentation the of hooks except http://codex.wordpress.org/Plugin_API#Current_Hooks_For_Actions.

Anyway, what would be the proper way to do this?

Related posts

1 comment

  1. Rather than “still on” page the user actually goes to the link and then sent back after post is deleted. From your description and quick look at code this is probably line that sends user back:

    wp_redirect( add_query_arg('deleted', 1, $sendback) );
    

    Since destination is filtered inside wp_redirect()

    $location = apply_filters( 'wp_redirect', $location, $status );
    

    and you have that ready-made deleted marker it should be easy to redirect user elsewhere, probably with engineering some checks so that it doesn’t interfere with normal admin operation.

Comments are closed.