简体   繁体   中英

WordPress delete link with redirect to home page

I have the following link which allows an author to delete a post on my site but I need it to redirect to the home page afterwards because at the moment it tries to take the user to the post itself which of cause will throw a 404 as it no longer exists (not very user-friendly)

Here is the code: <p id="delete"><a title="Delete your Favor?" href="<?php echo get_delete_post_link( $post->ID ); ?>">Delete your Favor?</a></p> <p id="delete"><a title="Delete your Favor?" href="<?php echo get_delete_post_link( $post->ID ); ?>">Delete your Favor?</a></p>

How can I modify it to redirect to the homepage?

According to the Wordpress 3.0.4 source code (wp-admin/post.php, line 223), going through that link calls the wp_trash_post function.

That function finishes by triggering a trashed_post action (wp-includes/post.php line 1838).

You can hook up your own handler to the trashed_post action (with add_action ) and do a wp_redirect.

Easiest way to do this: Your theme should have a functions.php file.

Add this to it:

add_action('trashed_post','my_trashed_post_handler',10,1);
function my_trashed_post_handler($post_id)
{
    wp_redirect( get_option('siteurl') );
    exit;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM