1

When anyone presses Publish or Update from the Block Editor in a CPT, I would like them to be redirected to the CPT listing page. (Users are typically editing many of the CPT at once, so this will save them a step - and also prevent them from pressing Update multiple times because they're not sure whether their edits have been saved.)

I've tried two different hooks:

From a 2013 question, redirect_post_location:

add_filter( 'redirect_post_location', 'wpse_124132_redirect_post_location' );
function wpse_124132_redirect_post_location( $location ) {
    if ( isset( $_POST['save'] ) || isset( $_POST['publish'] ) )
        return admin_url( 'edit.php?post_type=mycptslug' );
    return $location;
}

(I'm wondering if this only affects Core Posts since the hook has "post" in its name? It's nearly undocumented. Whether it changed since 2013 or just doesn't work with the new Editor, it didn't redirect.)

And from my own knowledge of the save_post hook:

add_action( 'save_post', 'wpse_redirect_to_dashboard', 40, 3 );
function wpse_redirect_to_dashboard( $post_id, $post, $update ) {
    global $_POST;
    // Since save_post runs 4 times, make sure this is the final run
    if( count( $_POST ) > 0 && true === $update && 'mycptslug' === $post->post_type ) {
        wp_redirect( admin_url( 'edit.php?post_type=mycptslug' ) ); exit;
    }
}

(I have confirmed the if condition is working as intended - I temporarily logged a success message to a text file, and it was succeeding once each time I pressed Update in the Editor - but it wouldn't redirect. Also tried redirecting to other URLs in case the admin_url() part wasn't working but still no redirect.)

Unfortunately, neither of these work with the Block Editor. The user still stays in the Editor after updating. Is there some other way, such as during CPT registration or some other hook, to successfully send the user on their way after they've made edits?


Based on cjbj's answer, I have this updated code - but it's still not redirecting. I am not quite sure what loading it late means or how to ensure this fires after the ajax save call. (This works for any post type though):

function wpse_redirect_to_post_list() {
    ?>
    <script>
        // Get post type from hidden input
        let postType=document.querySelector('form.metabox-base-form input#post_type').value;
        // On "publish / update / submit changes" button click, redirect to cpt listing
        jQuery( document ).ready( function( $ ) {
            var url = '/wp-admin/edit.php?post_type=' + postType;
            $( '.editor-post-publish-button__button' ).click( function() {
                window.location.href = url;
            });
        });
    </script>
    <?php
}
add_action( 'admin_print_footer_scripts', 'wpse_redirect_to_post_list' );
  • PHP doesn't hold the solution to your problem, to do this javascript would be required – Tom J Nowell Jul 23 at 15:37
0

As far as I can see, the function redirect_post, where the redirect_post_location filter resides, is not automatically called anywhere, so that filter won't be triggered. The save_post hook (or preferably save_post_post_type) still works, but not with metaboxes, as Gutenberg saves these separately using ajax calls. Ajax calls run in the background and do not affect the current page. As a result redirects don't work.

So, you cannot reliably redirect by calling the server. However, you can redirect on the user end by binding an extra jquery action to the save button. Something like this:

jQuery( document ).ready( function( $ ) {
    var url = 'your_url/edit.php?post_type=mycptslug';
    $( '.editor-post-publish-button__button' ).on( 'click',
        function() {
            window.location.href = url;
            });
        }
    );
});

Note: Didn't test the jquery, so might be buggy, but you get the picture. Make sure this is executed after the ajax save call by loading it late.

| improve this answer | |
  • Thanks, this gives me a better starting point. Right now I have pre-publish checks enabled, so I'll have to find a way to completely disable that to ensure that when they click publish all the db saving fires and they don't get redirected without the info actually being saved. – WebElaine Jul 23 at 18:35
  • cjbj, I have updated the question with my attempt at using your code - it's still not redirecting. Do I need to enqueue this with a certain priority instead of just printing it out, or how do you mean make sure it is executed after the ajax save call by loading it late? I greatly appreciate your help! – WebElaine Jul 23 at 21:30
  • I'm afraid this is a timing issue that I would have to solve through trial and error as well. Did you check if the jquery is called at all by replacing the redirect with an alert? There might be varying classes on the button. Or some other jquery function on the same selector disables click actions. – cjbj Jul 24 at 8:58
  • Aha - a console will fire successfully on doc ready, but isn't firing on button click. I'll keep playing with selectors and trying to see if something else is disabling click. – WebElaine Jul 27 at 14:19

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.