0

I created a delete button on frontend for blog posts and custom type posts and I would redirect to a specific page after deleting them, I looked around on google and I found all solutions like this:

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

The code works well ,but I don't want change the standard behavior of the backend so I did in this way:

add_action('deleted_post', 'woffice_trashed_post_handler', 10);
function woffice_trashed_post_handler($post_id) {
    if(!is_admin()) {
        wp_redirect( get_option('siteurl') );
        exit;
    }
}

This code doesn't work because I discovered that is_admin return always true, also if I delete a post by front end.

To create the deleting link by frontend I use this function:

get_delete_post_link(get_the_ID();

How can I solve it and redirect to a specific page after I delete a post just by front end or just by a specific link/button?

UPDATE: Thanks to zlatev reply I am able to trigger it and avoid the is_admin issue, now I have another very odd problem:

  1. I create a new post
  2. I open it and I click delete by fronend
  3. I am redirected to the blog listing page (my custom edirect url)
  4. The post is not deleted
  5. I hope the post again and I click delete again by frontend
  6. I am redirected to the same page
  7. The post is deleted on second time

I tested it many times and it's always deleted on second time, I create the deleting url with this and the url is always the same:

add_query_arg( 'origin', 'fe-blog', get_delete_post_link(get_the_ID(), '', true))

The code That I use for rdirect is this:

add_action('deleted_post', 'woffice_trashed_post_handler');
function woffice_trashed_post_handler() {
    if( isset( $_GET['origin'] ) && ($_GET['origin'] == 'fe-blog' || $_GET['origin'] == 'fe-wiki' || $_GET['origin'] == 'fe-project') ) {
        switch($_GET['origin']){
            case 'fe-blog':
                $blog_page = get_option( 'page_for_posts' );
                if(empty($blog_page)){
                    $pages = get_pages(array(
                        'meta_key' => '_wp_page_template',
                        'meta_value' => 'page-templates/blog.php'
                    ));
                    $blog_page = $pages[0]->ID;
                }
                //var_dump(get_permalink($blog_page));
                //die();
                wp_redirect( get_permalink($blog_page) );
                break;
        }
        exit();
    }
}

I am using a wrong hook?

0

More importantly you should notice what link is produced by get_delete_post_link(). It's an admin link is_admin() check will be always true. Instead you need something else that helps match the request. Try adding and query arg to your delete link.

add_query_arg( 'origin', 'fe', get_delete_post_link( get_the_ID() ) );

then in the action

add_action('deleted_post', 'woffice_trashed_post_handler', 10);
function woffice_trashed_post_handler($post_id) {
    if( isset( $_POST['origin'] ) && $_POST['origin'] == 'fe' ) {
        wp_redirect( get_option('siteurl') );
        exit;
    }
}
  • Hi Zlatav, thank you, that's solution is awesome, in this way I can trigger it perfectly. Now I have another poblem, maybe caused by a wrong use of the hook, I edit my question right now adding information :) – Antonio Apr 5 '16 at 9:59

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.