admin管理员组文章数量:1122846
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.
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.
Share Improve this question edited Oct 5, 2018 at 14:45 fuxia♦ 107k38 gold badges255 silver badges459 bronze badges asked Oct 5, 2018 at 14:42 Felix EveFelix Eve 2413 silver badges13 bronze badges3 Answers
Reset to default 0This 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);
}
}
Another Solution:
function wp_redirect_post_location( $location ) {
if ( 'post' == get_post_type() ) {
if ( isset( $_POST['save'] ) || isset( $_POST['publish'] ) )
return admin_url( "edit.php?post_type=post" );
}
return $location;
}
add_filter( 'redirect_post_location', 'wp_redirect_post_location' );
Copy code to your child theme function file. Copy everything from // to the last }}
//redirection after create
add_action('save_post','redirect_page');
function redirect_page(){
$type= get_post_type();
switch ($type){
case "post":
$url= admin_url().'edit.php?msg=post';
wp_redirect($url);
exit;
break;
case "product":
$url= admin_url().'edit.php?post_type=product&msg=page';
wp_redirect($url);
exit;
break;
case "page":
$url= admin_url().'edit.php?post_type=page&msg=page';
wp_redirect($url);
exit;
break;
} }
本文标签: How to after saving or publishing a post redirect back to the original page
版权声明:本文标题:How to after saving or publishing a post redirect back to the original page 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736282784a1926754.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论