How to after saving or publishing a post redirect back to the original page

This can be achieved with a simple custom plugin with 2 actions and 2 filters.

First use the post_row_actions filter to alter the edit link on the admin page to attach a custom query called final_destination with the URI of the page we are currently on.

Next we can use the edit_form_advanced action to add a hidden field to the post edit form with the value of the final_destination GET parameter.

Now we can use the redirect_post_location filter to check for the final_destination in the POST array and return the redirect if it’s set.

Finally we also need to edit the admin toolbar so the edit link there also has the final_destination parameter set. For this we can use admin_bar_menu action.

So the whole plugin would end up looking something like this:

<?php
/*
Plugin Name: Post Edit Redirects
Description: When viewing a post, and then clicking on edit post, after saving you post you should be redirected back
to the original page. Same deal with when you are on an admin screen viewing a list of posts and then click edit.
This module sorts all that out.
Version: 1.0
*/

// Alter the edit link on post listings pages to include a final destination so we can redirect here after editing is done.
add_filter('post_row_actions', 'post_edit_redirects_post_row_actions', 10, 1);
function post_edit_redirects_post_row_actions($actions) {
    $actions['edit'] = str_replace('action=edit', 'action=edit&final_destination=' . urlencode($_SERVER['REQUEST_URI']), $actions['edit']);
    return $actions;
}

// Alter the post edit form to include a hidden field for final destination
add_action('edit_form_advanced', 'post_edit_redirects_edit_form_advanced');
function post_edit_redirects_edit_form_advanced() {
    if (!empty($_GET['final_destination'])) {
        echo '<input id="final_destination" name="final_destination" type="hidden" value="' . urldecode($_GET['final_destination']) . '">';
    }
}

// After a post is saved check for a final destination to redirect to.
add_filter('redirect_post_location', 'post_edit_redirects_redirect_post_location');
function post_edit_redirects_redirect_post_location($location) {
    if (!empty($_POST['final_destination'])) {
        return $_POST['final_destination'];
    }
}

// Edit the admin toolbar so the edit link has the final_destination query string attached
add_action('admin_bar_menu', 'post_edit_redirects_admin_bar_menu', 999);
function post_edit_redirects_admin_bar_menu(WP_Admin_Bar $wp_admin_bar) {
    $all_nodes = $wp_admin_bar->get_nodes();

    // It is not possible to edit a node, so remove them all and put them back again, making the edit along the way.
    foreach($all_nodes as $key => $val) {
        $current_node = $all_nodes[$key];
        $wp_admin_bar->remove_node($key);

        if($key == 'edit') {
            $current_node->href .= '&final_destination=' . urlencode($_SERVER['REQUEST_URI']);
        }

        $wp_admin_bar->add_node($current_node);
    }
}

Leave a Comment