WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] 3.5 Cannot Delete CustomPost from Frontend anymore (7 posts)

  1. jibbius
    Member
    Posted 1 year ago #

    I updated to 3.5, and the functionality to delete Custom Posts (via the frontend), broke.

    Previously, I would generate a hyperlink, based on the following function:

    function wp_delete_post_link($text = '[delete]',$confirm_required=true) {
        global $post;
    
        $delLink = wp_nonce_url( site_url() . "/wp-admin/post.php?action=trash&post=" . $post->ID, 'trash-' . $post->post_type . '_' . $post->ID);
     	return $confirm_required
     		? '<a href="' . $delLink . '" onclick="javascript:if(!confirm(\'Are you sure you want to remove this post?\')) return false;" />'.$text."</a>"
     		: '<a href="' . $delLink . '">'.$text."</a>";
    }

    Now when I click the link, I am redirected to a "WordPress Failure Notice" page:

    Are you sure you want to do this?
    Please try again.

    I am running this install locally using DesktopServer, and have not upgraded my live site yet.
    To remedy, I have tried:
    - Restarting the server
    - Clearing my browser cache/cookies...etc
    - Disabling all plugins *except BuddyPress & a small one I needed to generate the link

    Nothing generated in error_log

  2. jibbius
    Member
    Posted 1 year ago #

    I appreciate this isn't an easy one to fix.
    The Buddypress factor probably doesn't help.

    I've tested the same function on non-custom posts; 2012 theme; within the index.php loop. Behaviour is as expected (i.e. post gets deleted).

    Will keep testing things, but if anyone has suggestions would be much appreciated.

    Cheers,

  3. jibbius
    Member
    Posted 1 year ago #

    Ok - so I think I have enough information for someone to replicate- (i.e. ALL plugins disabled; 2012 theme)

    Added this to 2012's archive.php (during the loop):

    echo wp_delete_post_link();

    Add this to the top of 2012's functions.php

    //Create locations custom post type
    add_action('init', 'create_locations');
    function create_locations() {
    	global $locations_options;
    	$location_labels = array(
    			'name' => _x('Locations', 'post type general name'),
    			'singular_name' => _x('Location', 'post type singular name'),
    			'add_new' => _x('Add New', 'location'),
    			'add_new_item' => __('Add New Location'),
    			'edit_item' => __('Edit Location'),
    			'new_item' => __('New Location'),
    			'all_items' => __('All Locations'),
    			'view_item' => __('View Location'),
    			'search_items' => __('Search Locations'),
    			'not_found' =>  __('No locations found'),
    			'not_found_in_trash' => __('No locations found in Trash'),
    			'parent_item_colon' => '',
    			'menu_name' => 'Locations'
    
    	);
    	$location_args = array(
    			'labels' => $location_labels,
    			'public' => true,
    			'show_ui' => true,
    			'hierarchical' => false,
    			'has_archive' => true,
    			'supports' => array('title', 'editor', 'thumbnail'),
    	);
    	if ( !post_type_exists('location') ){
    		register_post_type('location',$location_args);
    	}
    }
    
    function wp_delete_post_link($text = '[delete]',$confirm_required=true) {
    	global $post;
    
    	$delLink = wp_nonce_url( site_url() . "/wp-admin/post.php?action=trash&post=" . $post->ID, 'trash-' . $post->post_type . '_' . $post->ID);
    	return $confirm_required
    	? '<a href="' . $delLink . '" onclick="javascript:if(!confirm(\'Are you sure you want to remove this post?\')) return false;" />'.$text."</a>"
    			: '<a href="' . $delLink . '">'.$text."</a>";
    }

    Navigate to: http://www.example.dev/?post_type=location

    Attempt to use the [delete], and you see the reported error.

  4. jibbius
    Member
    Posted 1 year ago #

    Hmmm...

    Well, I found a fix... (incase anyone is interested).
    Just had to use get_delete_post_link( $post->ID) instead.

    function wp_delete_post_link($text = '[delete]',$confirm_required=true) {
    	global $post;
    
    	$delLink = get_delete_post_link( $post->ID);
    	return $confirm_required
    	? '<a href="' . $delLink . '" onclick="javascript:if(!confirm(\'Are you sure you want to remove this post?\')) return false;" />'.$text."</a>"
    			: '<a href="' . $delLink . '">'.$text."</a>";
    }
  5. Tanel
    Member
    Posted 1 year ago #

    Thanks jibbius, this helped me out, I had exactly the same problem.

    I think the error might have had something to do with the fact that I have an underscore ("_") in my custom post type's name. If you look closely at how the $delLink was constructed in the original wp_delete_post_link function, then you can see that it also inserts an underscore to the link generated.

  6. jibbius
    Member
    Posted 1 year ago #

    Haha no worries!
    Hopefully you spent less time on it than I did :)

    Hmm... yes, I never thought to actually compare the two different URLs, but the underscore issue could definitely be the cause.

    I am much happier using get_delete_post_link() anyway, far less that can go wrong.

  7. BenSteiner
    Member
    Posted 1 year ago #

    You just saved my life! Thank you so much!

Topic Closed

This topic has been closed to new replies.

About this Topic