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).
I am also getting the “The link you followed has expired.” error. I refreshed several times.
Hm, okay. Is the code in one of these files: index.php, page.php, single.php. If so, is it within the Loop?
am getting a “The link you followed has expired.” error
If you refresh the page does the error still appear?
There’s nothing here
Just updated the post with the snippet.
Thanks!