I am trying to enlist all my posts on the front-end along with delete and edit links (using the shortcode API). I am having difficulty with the delete links. To me the most obvious way to handle deletion is to use query variables and $_GET for deletion. However, if the user keeps reloading the page, WordPress will try to delete the post on every page load.

Another solution that comes to mind is to make each delete link a button and embed it in a form along with hidden fields containing post id and nonce etc. However this could potentially result in hundreds of form elements and an even higher number of hidden fields on a single page, which seems messy.

The last solution that I can think of is to use redirection. I could use query variables but intercept the page load, delete the post and redirect the user to the page without the query variables. However I can't seem to find the right hook for this. I want my function to run early enough so that I don't get any "headers already sent" messages.

I'd like to know which method is the correct one? And is there a hook that I can use for that last solution (something like the load hook but for the front-end).

Many thanks.

1 Answer 1


There are two more possible ways

  1. Use the browser's history API to manipulate the URL displayed at the address bar/ history.

  2. Use AJAX

Redirection is the best solution for 100% browser support and it is probably the easiest to implement. You don't need to use any hook with that just do the processing without generating output and then redirect back to the page. Something like

if (isset($GET['posttodelete'])) {
  delete post
  wp_redirect(utl with 'posttodelete' stripped out of it);
} else 
  normal loop;

But your users might prefer the AJAX solution which requires much more coding but gives the best user experience.

  • I already tried the wp_redirect method but I start getting "headers already sent" errors. I have faced this problem in the past as well. Commented Jul 28, 2014 at 5:29
  • you probably output the header before getting to the redirect code. You should probably place the code in your theme's function.php code and hook it on the 'init' action, but you can probably use it in the header.php file as well. Commented Jul 28, 2014 at 5:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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