Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Is there a hook I can use that when a post is created or updated it goes back to the list/table page of all the posts. If it is also possible to target a post type as well that would be cracking.

It's a really silly request I know and have argued the toss about it but wanted to make sure it was achievable if not very user friendly.

Steve

share|improve this question

1 Answer 1

Use the redirect_post_location filter and admin_url() function.

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

To redirect to a different url, add everything after the /wp-admin/ portion of the url. I used "edit.php" because the intended url was: http://example.com/wordpress/wp-admin/edit.php.


The redirect_post_location filter is not documented in the Codex Filter Reference. You can find it in the \wp-admin\post.php file near line 73. This is the WordPress code in the trunk version of WordPress:

wp_redirect( apply_filters( 'redirect_post_location', $location, $post_id ) );

As you can see, you can also test the $post_id redirect based on the $post_id or any information obtained from it. To use this second parameter of the filter, you need to pass the priority and _accepted_args_ parameters in the filter call:

add_filter( 'redirect_post_location', 'wpse_124132_redirect_post_location', 10, 2 );

And update the function parameters:

/**
 * 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;
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.