开发者

WordPress delete link with redirect to home page

开发者 https://www.devze.com 2023-02-09 09:08 出处:网络
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 wh

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>

How can 开发者_StackOverflow社区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;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消