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?
To redirect after the use of
get_delete_post_link()
it’s probably easiest to hook into thetrashed_post
action:Code:
Or you could make it dependent on the according
$_GET
variable by hooking into the the actionparse_request
:Code:
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, inlink-template.php
. There you’ll see how the$delete_link
is constructed. You can alter the return of the function via the correspondent filterget_delete_post_link
. This way you can make the link point to your custom page or endpoint for frontend post deletion.Code:
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.