X

How to Allow Users to Delete a Post From the Front-End

Snippets by IsItWP

Want to allow users to delete a certain post from the frontend of your WordPress site? This snippet comes in handy when you don’t want to provide the backend access to your users to delete a post.

Instructions:

All you have to do is add this code to your template file such as theme’s functions.php file such as index.php, page.php or in single.php within the loop to display a delete link for anyone with the rights to edit posts.:

<?php
$url = get_bloginfo('url');
  if (current_user_can('edit_post', $post->ID)){
    echo '<a class="delete-post" href="';
    echo wp_nonce_url("$url/wp-admin/post.php?action=trash&post=$id", 'delete-post_' . $post->ID);
    echo '">Delete post</a>';
  }
?>

Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly copy / paste code snippets in WordPress, so you don’t accidentally break your site.

If you liked this code snippet, please consider checking out our other articles on the site like: Best WordPress Page Builders (Compared).

Comments  Leave a Reply

  1. I am also getting the “The link you followed has expired.” error. I refreshed several times.

    1. Hm, okay. Is the code in one of these files: index.php, page.php, single.php. If so, is it within the Loop?

  2. am getting a “The link you followed has expired.” error

    1. If you refresh the page does the error still appear?

  3. There’s nothing here

    1. Just updated the post with the snippet.

      1. Thanks!

Add a Comment Cancel reply

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!
[60% OFF Today!]
[60% OFF Today!]
[50% OFF TODAY!]
[50% OFF TODAY!]
[60% OFF Today!]
[60% OFF Today!]
[10% OFF Today!]
[10% OFF Today!]
[50% OFF Today!]
[50% OFF Today!]