4

I have a custom post list in the admin panel. This list is paginated, so this is it's url :

  • wp-admin/edit.php?post_type=xxx, if I open the default page
  • wp-admin/edit.php?post_type=xxx&paged=n, if I go to the nth page of the list

Each time I delete permanently an item of that list I would like to be redirected to the same page of the list if this page still exists or to the previous page of the list if the same page is no longer relevant.

Let's say I have three pages for a total of 22 items where page one has 10 items, page two has 10 items and page three has 2 items.

  • if I delete the last item of page three, I should be redirected to page three (as there is still one item left there after deletion)

  • If I delete the last item of page three now, I should be redirected to page two (as now, page three doesn't exist anymore)

  • If I delete any item of page two, I should be redirected to page two (as page two still exists)

  • If I delete any item of page one, I should be redirected to page one (as page one still exists)

So far I'm just able to redirect to the default first page of the list like this:

function my_deleted_post_handler() 
{
    global $post;
    $post_type = $post->post_type; 
    wp_redirect("http://www.xxxxxx.com/wp-admin/edit.php?post_type=".$post_type);
    exit;   
}
add_action('deleted_post','my_deleted_post_handler');

But I don't know how to get the page number I'm on after deletion (or I was on before deletion), so I can't redirect to the relevant page afterwards.

I tried to retrieve $_GET['paged'], but the function above is triggered after the deletion when the "paged" variable is not part of the url anymore so it doesn't return any value.

How to achieve this?

1 Answer 1

1

You can use $_SERVER['HTTP_REFERER'] to get the URL the user came from.

The following code will find the paged GET variable of the previous URL to determine which page the user was on, then count how many posts are left of that particiular post type (only published) and how many pages are needed to display them all and then take the appropriate redirect action.

function my_deleted_post_handler() 
{
    global $post;

    // URL the user came from
    $referer = $_SERVER['HTTP_REFERER'];

    // the page the user was on
    $page = preg_match('/http:\/\/.*&paged=(\d*)/', $referer, $matches);
    $page_num = $matches[1];

    // how many published posts of $post_type?
    $post_type = $post->post_type; 
    $num_posts = wp_count_posts($post_type)->published;

    // how many pages of 10 items?
    $pages = ceil($num_posts / 10);

    if($pages < $page_num) {
        // the page the user was on doesn't exist anymore
        wp_redirect('http://www.xxxxxx.com/wp-admin/edit.php?post_type=' . $post_type . '&paged=' . $page_num - 1);
    } else {
        // the page still exists
        wp_redirect('http://www.xxxxxx.com/wp-admin/edit.php?post_type=' . $post_type . '&paged=' . $page_num);
    }

    exit();   
}
add_action('deleted_post','my_deleted_post_handler');

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.