0

I've created a custom post type with front-end submission. I added a delete link using get_delete_post_link ... which works great, except it brings me to a "Page not found" after the post is deleted.

I'd like to be able to redirect to another page after the post is deleted. I know I can use the add_filter( 'get_delete_post_link', 'some_function' ) ... but I wouldn't want that to fire every time that function is run in case I create other custom post types with front end submisison.

I know about wp_delete_post... but I can't call that from a link.

What would be the best way to do this? Can't find any real solutions after some reading.

Thanks!

1 Answer 1

1

I would create an AJAX Call from the delete post Link.

First, register the AJAX function:

add_action( 'wp_ajax_wpse_delete_post', 'wpse_delete_post' );

I would not add the AJAX function to the nopriv Users, but if you want every visitor of the site (ignoring capabilities) to be able to delete a post, you would have to add this as well:

add_action( 'wp_ajax_nopriv_wpse_delete_post', 'wpse_delete_post' );

The next thing to do is define the callback-function:

function wpse_delete_post() {

    // be sure to add all the security you need, so that no post gets deleted by accident or malicious intent
    $postid = intval( $_POST['post_id'] );
    // also define what you want your AJAX message to return
    if ( wp_delete_post( $postid ) === false ) {
        echo "fail";
    } else {
        echo "success";
    }

    die(); // needed to function properly

}

The final step would be the the AJAX call from your Link. Lets assume you have the ID available on your Link, just to make things easier:

<a href="#" id="POSTID" class="ajax_delete_post">delete post</a>

You add the following Javascript to your functions:

jQuery(document).ready(function($){

    $(".ajax_delete_post").click( function(){
        var data =  {
            action: 'wpse_delete_post',
            post_id: $(this).val("id")
        };
        $.post(ajaxurl, data, function(response) {
            console.log(response);
        });

    });

}); 

Be sure to have ajaxurl defined as well, and you should be good to go.

3
  • Thanks for your response. I'm not super experienced with jQuery just yet. Looking over .post... I'm a little confused as to what it does and how I use it here. Also, I'm assuming I redirect in wpse_delete_post() if wp_delete_post is successful right?
    – Josh
    Commented Jan 16, 2013 at 16:16
  • Nono, the wpse_delete_post() is in the AJAX call - so it does not happen on the website. in the jQueryfunction you tell the server to call this function, and the part with console.log outputs the return value of the function in your console.
    – fischi
    Commented Jan 16, 2013 at 18:03
  • So, I got this to work after doing some more reading on it. I decided to remove the post div instead of redirect. Had to use the $(this).attr("id") instead of .val() ... I used the response to make sure the response matched the post-id of the div I wanted to remove... and then I removed it! Thanks for pointing the direction!
    – Josh
    Commented Jan 21, 2013 at 14:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.