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:

Share This Post Share This Post SubscribePrint This Post Print This PostSave As PDF Save As PDF

Related Posts Hot Posts Hot Comments Hot Tags

      1 Comment Add your own

      Leave a Comment

      Required

      Required, hidden

      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


      Calendar

      February 2011
      MTWTFSS
      « Jan Mar »
       123456
      78910111213
      14151617181920
      21222324252627
      28 

      Sponsors

      Most Recent Posts