Whenever I update a term it currently redirects me back to the edit-tags.php page for that taxonomy, the term list. Is it possible to instead of redirects to the term list to instead bring me back to the same term I just updated? Similar to how updating a post will bring you back to the same post you just updated.

After looking at the Edit Tags Form on the surface it doesn’t look like there’s a way to modify the action or change where it’s going but after actually looking at the URLs it seems to redirect based on $_GET parameters. For example, a taxonomy list url looks like:

/wp-admin/edit-tags.php?taxonomy=taxonomy_name&post_type=type_name

And whenever editing a term, it just passes the ‘tag’ ID:

/wp-admin/edit-tags.php?action=edit&taxonomy=taxonomy_name&tag_ID=123&post_type=type_name

So I would need to find a filter to on the edit term page that would pass in the same term ID that the current user is editing. How would I go about achieving this? Suggestions on filters?

2 Answers
2

I agree with you, it’s somewhat an unexpected user experience, when you’re redirected after updating a term.

Additionally there seems to be no error handling on the form itself, if you try to make some errors, like deleting the term name and slug, it will not show any errors, just ignore your changes and redirect.

You can try the following to stay on the same edit term page after updating:

/**
 * Stay on the edit term page after updating.
 * @see http://wordpress.stackexchange.com/a/168206/26350
 */

add_filter( 'wp_redirect', 
    function( $location ){
        $mytaxonomy = 'post_tag'; # <-- Edit this to your needs!
        $args = array(
            'action'   => FILTER_SANITIZE_STRING,
            'taxonomy' => FILTER_SANITIZE_STRING,
            'tag_ID'   => FILTER_SANITIZE_NUMBER_INT,
        );
        $_inputs    = filter_input_array( INPUT_POST, $args );
        $_post_type = filter_input( INPUT_GET, 'post_type', FILTER_SANITIZE_STRING );
        if( 'editedtag' === $_inputs['action'] 
            && $mytaxonomy === $_inputs['taxonomy']
            && $_inputs['tag_ID'] > 0
        ){
            $location = add_query_arg( 'action',   'edit',               $location );
            $location = add_query_arg( 'taxonomy', $_inputs['taxonomy'], $location );
            $location = add_query_arg( 'tag_ID',   $_inputs['tag_ID'],   $location );
            if( $_post_type )
                $location = add_query_arg( 'post_type', $_post_type, $location );
        }
        return $location;
    }
);

where you have to modify the $mytaxonomy value to your needs.

You might also want to add a Go back link to the form, for example:

/**
 * Add a "Go back" link to the post tag edit form.
 * @see http://wordpress.stackexchange.com/a/168206/26350
 */

add_action( 'post_tag_edit_form',
    function( $tag ) {
        $url = admin_url( 'edit-tags.php' );
        $url = add_query_arg( 'tag_ID',   $tag->term_id, $url );
        $url = add_query_arg( 'taxonomy', $tag->taxonomy, $url );
        printf( '<a href="https://wordpress.stackexchange.com/questions/168193/%s">%s</a>', $url, __( 'Go back' ) );
    }
);

where we use the {$taxonomy}_edit_form hook. It would probably make more sense to add the link directly under the submit button, or in the same line, not above it like we do here, but I couldn’t find any suitable hook for that. You could of course use CSS to change the link’s position.

Leave a Reply

Your email address will not be published. Required fields are marked *