0

I use the following link to delete a post in the frontend of wordpress:

<a href="<?php echo get_delete_post_link( $post->ID ) ?>">Delete Post</a>

This works fine. But after i delete the post its just showing a blank page of the index.php. I want to redirect the author who deletetd the post to a category site like /post-archive. Any idea how i can do that?

Thx for your help and best regards.

3

I got a little bit more into this topic and found this solution which works perfect for me.

1 Add this code to functions.php:

// Delete post
function delete_post(){
    global $post;
    $deletepostlink= add_query_arg( 'frontend', 'true', get_delete_post_link( get_the_ID() ) );
    if (current_user_can('edit_post', $post->ID)) {
        echo       '<span><a class="post-delete-link" onclick="return confirm(\'¿Are you sure to delete?\')" href="'.$deletepostlink.'">Borrar</a></span>';
    }
}

//Redirect after delete post in frontend
add_action('trashed_post','trash_redirection_frontend');
function trash_redirection_frontend($post_id) {
    if ( filter_input( INPUT_GET, 'frontend', FILTER_VALIDATE_BOOLEAN ) ) {
        wp_redirect( get_option('siteurl').'/page-deleted-post' );
        exit;
    }
}

2 Call the function on youre template file (single.php or whatever):

echo delete_post();
2

Here's one approach by modifying the redirect location after the post has been trashed:

/**
 * Redirect to the home url after trashing a post on the front-end
 *
 * @link http://wordpress.stackexchange.com/a/212146/26350
 */
add_action( 'trashed_post', function()
{
    add_filter( 'wp_redirect', function( $location, $status )
    {
        if (   is_wpse_part_of_admin_url( $location ) ) 
            && 1 == get_wpse_query_arg( $location, 'trashed' )
        )
            $location = esc_url( home_url() ); // Adjust to your needs!

        return $location;
    } );
} );

where we make sure that the redirect location is not within the backend with the help of our is_wpse_part_of_admin_url() helper function.

There are handy functions like add_query_arg() and remove_query_arg() but not get_query_arg(). So here's our helper function:

/**
 * Get query argument from an url
 *
 * @uses wp_parse_str()
 * @param string $url 
 * @param string $arg
 * @return string|false
 */
function get_wpse_query_arg( $url, $arg )
{
    wp_parse_str( parse_url( $url, PHP_URL_QUERY ), $args );

    if( isset( $args[$arg] ) )
        return $args[$arg];

    return false;
}

Here's our helper function to determine if the url is a backend url:

/**
 * Check if the url starts with admin_url()
 *
 * @uses admin_url()
 * @param string $url 
 * @return boolean
 */
function is_wpse_part_of_admin_url( $url )
{
    return 0 === strpos( $url, admin_url() );
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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