WordPress “Delete Post Link”
February 16th, 2011 at 11:41am Jason 270 views
This WordPress codes for the user to delete posts when browsing the front-end of the site. It will also show a pop-up confirmation box to appear before deleting the post. So here it is:
» a link to delete (or “trash”) a post from the front-end
» shows only if the user is logged in and has the capability to delete that post
» shows a confirmation pop-up box
» if confirmed, post is trashed and page is refreshed
The following goes in the theme’s functions.php file:
function wp_delete_post_link($link = 'Delete This', $before = '', $after = '') { global $post; if ( $post->post_type == 'page' ) { if ( !current_user_can( 'edit_page', $post->ID ) ) return; } else { if ( !current_user_can( 'edit_post', $post->ID ) ) return; } $message = "Are your sure you want to delete ".get_the_title($post->ID)." ?"; $delLink = wp_nonce_url( get_bloginfo('wpurl') . "/wp-admin/post.php?action=delete&post=" . $post->ID, 'delete-post_' . $post->ID); $link = "<a href='" . $delLink . "' onclick = \"if ( confirm('".$message."' ) ) { execute(); return true; } return false;\"/>".$link."</a>"; echo $before . $link . $after; }
Then use the following template tag wherever you want it to appear in your theme.
<?php wp_delete_post_link('trash', '<p>', '</p>'); ?>
via: http://dynabytes.net/wordpress/wordpress-delete-post-link/
Entry Filed under: WordPress Tagged with: wordpress
Hot Posts Hot Comments Hot Tags
- apartment bbpress blog class CMS code CSS div dream english friends girl Google hot Hot Dog html job JS life lines love Madonna menu Michael Jackson movie oral english PHP plugin saying school sexy site student students submenu success tags teacher teaching txt university video website widget wordpress





1 Comment Add your own
1. Peer | September 2nd, 2011 at 10:08 pm
This works great with confirmation,
but if I navigate to single post, like
http://localhost/someblog/2011/09/02/delete-me/
after the post is deleted , and page gets refreshed saying
This is somewhat embarrassing, isn’t it?
http://localhost/someblog/2011/09/02/delete-me/?deleted=1
Any idea?
Leave a Comment
Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">
Trackback this post | Subscribe to the comments via RSS Feed