0

When viewing a list of posts on the admin page wp-admin/edit.php I click on edit. I then finish editing the post, click on save or publish and expect to be returned to the original admin listing page I started on.

The same with when I'm viewing the front end of a post and click the edit link in the admin toolbar. After I finish editing the post I want to be returned to the original screen I was on.

To me this seems like this is the most predictable behavior and I'm surprised WordPress does not act like this out the box.

This behavior should be consistent with all custom post types.

0

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);
    }
}

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.