使用redirect_post_location
过滤器和admin_url()
作用
add_filter( \'redirect_post_location\', \'wpse_124132_redirect_post_location\' );
/**
* Redirect to the edit.php on post save or publish.
*/
function wpse_124132_redirect_post_location( $location ) {
if ( isset( $_POST[\'save\'] ) || isset( $_POST[\'publish\'] ) )
return admin_url( "edit.php" );
return $location;
}
要重定向到其他url,请在
/wp-admin/
url的一部分。我用过
"edit.php"
因为预期的url是:
http://example.com/wordpress/wp-admin/edit.php
.
The
redirect_post_location
过滤器未记录在
Codex Filter Reference. 你可以在
\\wp-admin\\post.php
第73行附近的文件。这是WordPress主干版本中的WordPress代码:
wp_redirect( apply_filters( \'redirect_post_location\', $location, $post_id ) );
如您所见,您还可以测试
$post_id
重定向基于
$post_id
或从中获得的任何信息。要使用过滤器的第二个参数,需要在
filter call:
add_filter( \'redirect_post_location\', \'wpse_124132_redirect_post_location\', 10, 2 );
并更新功能参数:
/**
* Redirect to the edit.php on post save or publish.
*/
function wpse_124132_redirect_post_location( $location, $post_id ) {
if ( isset( $_POST[\'save\'] ) || isset( $_POST[\'publish\'] ) ) {
// Maybe test $post_id to find some criteria.
return admin_url( "edit.php" );
}
return $location;
}