[resolved] Front End Delete Post with Custom Post Type (16 posts)

  1. Clicknathan
    Posted 4 years ago #

    I'm trying to implement a front end delete link such as the one found here (and similar code on other forum posts): http://wordpress.org/support/topic/delete-post-link?replies=11

    However, though that works for pages & posts, it returns a WordPress Failure screen for custom post types.

    Would anyone know how to make this work for custom post types? Perhaps it's something I need to do when registering the custom post type?

    Here's my code for registering the post type:

    add_action('init', 'wishlist_register');
    function wishlist_register() {
    	$labels = array(
    		'name' => _x('Wishlist Items', 'post type general name'),
    		'singular_name' => _x('Wishlist Item', 'post type singular name'),
    		'add_new' => _x('Add New', 'wishlist item'),
    		'add_new_item' => __('Add New Wishlist Item'),
    		'edit_item' => __('Edit Wishlist Item'),
    		'new_item' => __('New Wishlist Item'),
    		'view_item' => __('View Wishlist Item'),
    		'search_items' => __('Search Wishlist'),
    		'not_found' =>  __('Nothing found'),
    		'not_found_in_trash' => __('Nothing found in Trash'),
    		'parent_item_colon' => ''
    	$args = array(
    		'labels' => $labels,
    		'public' => true,
    		'publicly_queryable' => true,
    		'show_ui' => true,
    		'query_var' => true,
    		'rewrite' => true,
    		'capability_type' => 'post',
    		'hierarchical' => false,
    		'menu_position' => null,
    		'has_archive' => true,
    		'supports' => array('title','editor','thumbnail', 'author')
    	register_post_type( 'wishlist' , $args );
  2. deepbevel
    Posted 4 years ago #

    You might try a plugin which registers custom posts, as an alternative to the code, just to see.

    This looks interesting, I too made a wish list system, I used custom posts, and my delete link worked. But I used a plugin to register the custom posts.

    I even explain how I made it on the site. My delete link code should be there as well.

  3. Clicknathan
    Posted 4 years ago #

    Thanks DeepBevel, but it looks like you're using some type of "custom post template", not custom post types.

  4. deepbevel
    Posted 4 years ago #

    ooops, didn't know there was a difference. Hope you find something that works.

  5. Clicknathan
    Posted 4 years ago #

    Figured this out if anyone's interested. Turns out WP has a built in function for this. Here's my complete solution:

    What I'm doing with the code below is displaying the Delete link on the front end only if the user viewing the post is logged in as the post's author.

    <?php if ($post->post_author == $current_user->ID) { ?><p><a onclick="return confirm('Are you SURE you want to delete this Wish?')" href="<?php echo get_delete_post_link( $post->ID ) ?>">Delete post</a></p><?php } ?>

    More details on how that works here: http://goo.gl/iSP8l

  6. deepbevel
    Posted 4 years ago #

    yep, I have the same, never knew there was an issue with deleting custom posts types because I never used them.

    How are you compliing your lists? what's your system?
    I use a button-click function that allows shopping cart items to be copied as a posts to a private category/archive. the lists are organized by tag, and I have a tag search as the only means of accessing lists.

  7. Clicknathan
    Posted 4 years ago #

    This is a different kind of wishlist, Deepbevel. It allows non-profit organizations to post items they want (ie, their "wishes") to the site, and other users to contact them if they have the item. I needed them to be able to be deleted by the non-profits after they'd received the item, without logging into the backend.

  8. deepbevel
    Posted 4 years ago #

    great, and did it turn out the custom post had nothing to do with the delete code? I'm concerned as I may want to use custom post types at some point, but fortunately your code looks like mine.

  9. Clicknathan
    Posted 4 years ago #

    Well, using the code linked to in my original post, yes, it did have something to do with custom post types it appears, related to how the wp_nonce_url was being called.

    Using my latest code though, it works with custom post types.

  10. Louis_Dea
    Posted 4 years ago #

    Awesome Clicknathan. Thank you for posting the solution!

  11. ralphonz
    Posted 4 years ago #


    thanks for the post.

    Does anyone know how to redirect the user after they've deleted the post? At the moment they just see the post page again which is very misleading, i'd like them to return to the category page or see a "your post has been deleted" message.



  12. deepbevel
    Posted 4 years ago #

    I was never able to get a specific redirectiom, so I only made the delete link accessible on the category page, and not for single post view. I did finally get code to get a confirmation message before delete.

    I'll dig around and post if I can find it.

  13. alesandro85
    Posted 3 years ago #

    Hy Clicknathan,

    For a newbee like me one question regarding you code above:

    Where do I have to put that code into? functions.php?

    Thank you.


  14. deepbevel
    Posted 3 years ago #

    this is what I did, In archive.php inside the loop

    <div class="entry">
    <?php wp_delete_post_link('Delete this entry', '<p>', '</p>'); ?>
    (output here)

    in functions.php

    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 ) )
      } else {
        if ( !current_user_can( 'edit_post', $post->ID ) )
        $message = "Are you sure you want to delete ".get_the_title($post->ID)." ?";
        $delLink = wp_nonce_url( get_bloginfo('url') . "/wp-admin/post.php?action=delete&post=" . $post->ID, 'delete-post_' . $post->ID);
        $htmllink = "<a href='" . $delLink . "' onclick = \"if ( confirm('".$message."' ) ) { execute(); return true; } return false;\"/>".$link."</a>";
        echo $before . $htmllink . $after;
  15. Clicknathan
    Posted 3 years ago #

    @alesandro85, deepbevel is correct - the code goes in your theme's files, such as single.php or page.php where you want the edit link to appear.

  16. alesandro85
    Posted 3 years ago #

    @ Clicknathan, works well :) Thank you for the explanation.

Topic Closed

This topic has been closed to new replies.

About this Topic


No tags yet.