Automatically generate a delete post link and provide it to post creator

I thought of using get_delete_post_link(), but I have no clue how to get the correct link to the correct post and where to implement this. Maybe using an event listener? I am so lost..

Either passing in the post ID, or making sure you’re inside a post loop would work, but since you mentioned this is for logged out users, this function is a non-starter because it will send users to WP Admin.

So instead, you have 2 options:

  1. Force users to be logged in, and set that user as the author
  2. Save a unique identifier in the post data then pass it back along with the post ID and a nonce. Then you can make a request containing all 3 items to verify it is indeed the author deleting the post

Warning: Without some form of verification, anybody will be able to delete anything on your site just by running through every possible post ID. This is trivial to do.

How you might implement that request can take 2 forms:

  1. Send the user to the same page, and put URL parameters with the post ID and verification values. Then in functions.php look for those values, verify the user can do that, delete the post via wp_delete_post, then redirect onwards
  2. In JS, when the user clicks the delete link/button, make an authenticated REST API request to delete the post, passing along the post ID and any needed information. If it’s succesful, redirect the user. If it’s not succesful, display a message

If the user is logged in, the verification data part can be skipped. If you insist on logged out users, then you’ll need a custom REST API endpoint for option 2.

Contact Form 7

This is the more difficult part, how you might save a verification key to the post CF7 creates, or pass that into the response CF7 gives, is not something this site can cover. For this part you will need to contact CF7 support avenues, be that official support, communities, meetup groups, FB groups, etc