Ready to get started?Download WordPress


[resolved] 'delete post' on front end? (8 posts)

  1. baduist3
    Posted 7 years ago #

    i'm working on a template for my new blog, and trying to get a link to 'delete this post' for each post on the front end (in the blog), visible only to me when i'm logged in, of course.
    something like the 'edit this post' link that is already showing in kubrick template.

    Can anyone give tips about this?

  2. whooami
    Posted 7 years ago #


    Look at how its done in the backend. Under manage >> posts.

  3. baduist3
    Posted 7 years ago #

    i copied this code from there, but instead of deleting the post, i get a "Sorry, no posts matched your criteria".

    <?php if ( current_user_can('delete_post',$post->ID) ) { echo "<a href='" . wp_nonce_url("post.php?action=delete&amp;post=$id", 'delete-post_' . $post->ID) . "' class='delete' onclick=\"return deleteSomething( 'post', " . $id . ", '" . js_escape(sprintf(__("You are about to delete this post '%s'.\n'OK' to delete, 'Cancel' to stop."), get_the_title())) . "' );\">" . __('Delete') . "</a>"; } ?>

    i was using it in the loop, if it makes any difference.

  4. whooami
    Posted 7 years ago #

    well you cant copy that verbatim because what you copied isnt using the variables as they exist inside the loop.

    You need to adjust that so that when you are calling the post, you use it's variable as its assigned there -- look at how the edit link is handled, in other words.

  5. baduist3
    Posted 7 years ago #

    This code in the loop seems to do the trick:
    <?php if (current_user_can('edit_post', $post->ID)) echo "<a href='" . wp_nonce_url("/wp-admin/post.php?action=delete&amp;post=$id", 'delete-post_' . $post->ID) . "'>Delete post</a>" ?>

    Thanks whooami!

  6. whooami
    Posted 7 years ago #

    youre welcome :)

  7. gnomic
    Posted 7 years ago #

    I've added that bit of code to single.php, but wp redirect back to the page that was just deleted, resulting in a 404. How can I avoid this and have it go back to the home page?

  8. thepete
    Posted 6 years ago #

    Brilliant solution! Thank you!! The only thing that scares me is that there is no "are you sure?" alert before the post is actually deleted. Still, I'm a big boy. I can deal with the responsibility. :)


Topic Closed

This topic has been closed to new replies.

About this Topic