{"id":74926,"date":"2012-03-27T11:00:25","date_gmt":"2012-03-27T15:00:25","guid":{"rendered":"http:\/\/wpmu.org\/?p=74926"},"modified":"2012-03-26T20:14:50","modified_gmt":"2012-03-27T00:14:50","slug":"how-to-add-a-delete-post-link-to-your-wordpress-posts","status":"publish","type":"post","link":"https:\/\/wpmudev.com\/blog\/how-to-add-a-delete-post-link-to-your-wordpress-posts\/","title":{"rendered":"How to Add a Delete Post Link to Your WordPress Posts"},"content":{"rendered":"<p><a rel=\"lightbox[74926]\" class=\"blog-thumbnail\" href=\"https:\/\/wpmudev.com\/blog\/how-to-add-a-delete-post-link-to-your-wordpress-posts\/delete-big\/\" rel=\"attachment wp-att-74929\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-74929\" title=\"delete-big\" src=\"https:\/\/wpmudev.com\/blog\/wp-content\/uploads\/2012\/03\/delete-big.jpg\" alt=\"Post image\" aria-hidden=\"true\" width=\"631\" height=\"511\" \/><\/a><br \/>\nSome sites, especially those that allow contributors to post, may need an easy way to delete posts from time to time.<\/p>\n<p>In order to make it as easy as possible, of course, the best thing to do would be to put a \u201cDelete\u201d button right on the post itself. Of course this delete link should only be viewable by logged in users with a certain permission level, much like an \u201cEdit\u201d button that many themes use.<\/p>\n<p>Here\u2019s how you can go about doing that.<\/p>\n<p>First, place the following code in you functions file after the opening &lt;php tag. (Appearance &gt; Editor &gt; Theme Functions \u2013 functions.php)<\/p>\n<p><code>function wp_delete_post_link($link = 'Delete This', $before = '', $after = '')<br \/>\n{<br \/>\nglobal $post;<br \/>\nif ( $post-&gt;post_type == 'page' ) {<br \/>\nif ( !current_user_can( 'edit_page', $post-&gt;ID ) )<br \/>\nreturn;<br \/>\n} else {<br \/>\nif ( !current_user_can( 'edit_post', $post-&gt;ID ) )<br \/>\nreturn;<br \/>\n}<br \/>\n$link = \"&lt;a href='\" . wp_nonce_url( get_bloginfo('url') . \"\/wp-admin\/post.php?action=delete&amp;amp;post=\" . $post-&gt;ID, 'delete-post_' . $post-&gt;ID) . \"'&gt;\".$link.\"&lt;\/a&gt;\";<br \/>\necho $before . $link . $after;<br \/>\n}<\/code><\/p>\n<p>&nbsp;<\/p>\n<p>Next, you will need to put some code in your theme files where you would like your delete link to show up.<\/p>\n<p>I put the following code in my single.php code just above where the content of the post starts. (Appearance &gt; Editor &gt; Single Post &#8211; single.php)<\/p>\n<p><code>&lt;?php wp_delete_post_link('Delete This', '&lt;p&gt;', '&lt;\/p&gt;'); ?&gt;<\/code><\/p>\n<p>&nbsp;<\/p>\n<p>The <code>'&lt;p&gt;', '&lt;\/p&gt;'<\/code> in the code gives it space above it and below, but if you don\u2019t want that space, then you can take it out:<\/p>\n<p>&nbsp;<\/p>\n<p><code>&lt;?php wp_delete_post_link('Delete This'); ?&gt;<\/code><\/p>\n<p>&nbsp;<\/p>\n<p>You could also put this on your Main Index Template, for example, if you wanted the link to show up on the home page too. (Appearance &gt; Editor &gt; Main Index Template &#8211; index.php)<\/p>\n\n<p>And here\u2019s what it looks like.<\/p>\n<p><a rel=\"lightbox[74926]\" class=\"blog-thumbnail\" href=\"https:\/\/wpmudev.com\/blog\/how-to-add-a-delete-post-link-to-your-wordpress-posts\/delete-link\/\" rel=\"attachment wp-att-74930\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-74930\" title=\"delete-link\" src=\"https:\/\/wpmudev.com\/blog\/wp-content\/uploads\/2012\/03\/delete-link.jpg\" alt=\"Post image\" aria-hidden=\"true\" width=\"632\" height=\"978\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>Thanks to web-templates.nu for the code.<\/p>\n\n<p>Photo: <a href=\"http:\/\/www.bigstockphoto.com\/image-14364014\/stock-vector-delete-sticker-icon-vector-illustration\" target=\"_blank\">Delete sticker icon. Vector illustration.<\/a> From BigStock<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Need an easy way to delete posts? Here\u2019s your answer.<\/p>\n","protected":false},"author":84404,"featured_media":74928,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"blog_reading_time":"","wds_primary_category":0,"wds_primary_tutorials_categories":0,"footnotes":""},"categories":[4],"tags":[],"tutorials_categories":[],"class_list":["post-74926","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-plugins"],"_links":{"self":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts\/74926","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/users\/84404"}],"replies":[{"embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/comments?post=74926"}],"version-history":[{"count":2,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts\/74926\/revisions"}],"predecessor-version":[{"id":216260,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts\/74926\/revisions\/216260"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/media\/74928"}],"wp:attachment":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/media?parent=74926"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/categories?post=74926"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/tags?post=74926"},{"taxonomy":"tutorials_categories","embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/tutorials_categories?post=74926"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}