4

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?

0

2 Answers 2

4

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="%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.

5
  • 1
    Neat! I actually changed it a bit to apply to all taxonomies via $taxonomies = get_taxonomies(); then in the conditional: in_array( $_inputs['taxonomy'], $taxonomies ) for anybody else who would like to use this. Thanks!
    – Howdy_McGee
    Commented Nov 12, 2014 at 15:27
  • Thanks for sharing the extension to all taxonomies @Howdy_McGee.
    – birgire
    Commented Nov 12, 2014 at 16:54
  • I kept running into the problem on Custom Taxonomies where WP would think that it was a post type of post since there's no post type set in the URL. So it may need to check if $_GET['post_type'] is set, then set it into the URL, or you can use your other answer on getting taxonomy post types. Most will only notice a visual change ( menus ) but when dealing with custom fields it turned out to be a problem. Just a note for future readers :) !
    – Howdy_McGee
    Commented Dec 18, 2014 at 18:15
  • Looks like this is now default WP behavior and this hack is no longer needed. Commented Apr 25 at 21:43
  • Thanks for the update, there was also later added a filter here core.trac.wordpress.org/ticket/36367 @ArtemRussakovskii
    – birgire
    Commented Apr 25 at 22:28
0

Thanks @birgire for solution, I have modified above code for my need. Now we dont need to mention specific taxonomy or post type and instead this will work for all built_in and custom public taxonomies. I have tested it with custom post type and taxonomies.

Just copy it in child-theme functions.php and edit any taxonomy term or built_in taxonomy term e.g categories or tags to check

/**
 * Stay on the edit term page after updating.
 * Work for any post type and all custom/built_in taxonomies
 */

add_filter( 'wp_redirect', 
    function( $location ){
            $args = array(
              'public'   => true,
              '_builtin' => true

            ); 
        $taxonomy = get_taxonomies($args); //get all taxonomies
        foreach ($taxonomy  as $mytaxonomy) {
            $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'] 
                or $mytaxonomy === $_inputs['taxonomy']
                or $_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;
        }
    }
);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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