I have a custom taxonomy "stat groups" (non-hierarchical) for my custom post type "stats".

I created a link on the edit page for the chosen stat group term on the edit page for a stat.

After clicking the link and submitting the changes to that term, wp redirects to the edit-tags.php but without the post type in the query string... So the edit page comes up correctly, but in the navigation, it is showing the expanded "Posts" section instead of my custom post type's.

This is particularly annoying because I have custom css for the custom taxonomy's edit page, because there are added fields, and it is just messing it all up when it doesn't include the post type in the query string

I am guessing this is happening because of the http_referrer?

I cannot figure out how to get it to redirect back to the custom post edit page on saving the custom taxonomy term.

I have tried passing the redirect url in the query string to the edit page for the term, capturing it from the $_GET var, putting it in a hidden input value, then capturing it from the $_POST on save and trying to redirect using it after the save but it's not working. I've tried using wp_redirect as well as redirect canonical there.

So what I really want to do is for it to redirect back to the referring stats edit page for the post it was on after saving the edits to the custom tax term. If I can't do that, I'd at least like it to redirect to the right edit-tags page.

Anyone? Thanks for any help in advance! :)


Got it!

I'm not sure exactly what the culprit was, but I did a few things that resulted in it working.

Here's a summary of the code I used:

I defined this helper function based on code found in redirect_cannonical()

function get_current_url() {

    $current_url  = is_ssl() ? 'https://' : 'http://';
    $current_url .= $_SERVER['HTTP_HOST'];
    $current_url .= $_SERVER['REQUEST_URI'];

    return $current_url;

In my CPT metabox, I had a link at the bottom that was directed at the edit-tags.php page for my custom taxonomy term:

echo '<p><a class="sg_edit_link" href="' . admin_url( 'edit-tags.php?action=edit&taxonomy=stat_groups&post_type=stats&tag_ID='.$args['id'] ).'&redirect='.urlencode( get_current_url() ).'">Edit ' . $args['name'] . ' Stat Group</a></p>';

this is a dynamically created metabox (one for each term), so $args is the array of passed callback parameters for the add_meta_box() function.

So the link passes the current url as a 'redirect' parameter in the query string.

On the custom tax term edit page, there are added fields to the form. I add these lines to capture the redirect url, and add it to the form if it's there:

if ( isset($_GET['redirect']) ) {
    echo '<input type="hidden" name="redirect" value="'.esc_url($_GET['redirect']).'">';

Now that will get posted when the fields are saved, so in the function that saves the extra field data, I added this right after the option is updated:

if (isset($_POST['redirect'])) {

    $redirect = esc_url_raw( $_POST['redirect'] );

    wp_redirect( $redirect );


One of the things that I had left out that could have been the only thing causing it not to work was the exit; in the last code block there. This is pointed out as pretty important to include in the codex for wp_redirect() but I guess I just missed it :/

This was my solution, I would be interested to know if there is a more elegant way to do it, although this isn't too bad.

  • Nice! :) Just to be sure escape the request data before you print them into your HTML output. I recommend esc_url() and esc_attr(). – fuxia May 7 '12 at 2:27
  • @toscho Thanks! I used urlencode for the redirect url in the 2nd code box above, is that what you're referring to? Where else should it be escaped - in the value attribute of the redirect hidden field? Should esc_url() always be used instead of urlencode or is there a proper use case for each? – Evan Mattson May 7 '12 at 3:44
  • urlencode() just prepares characters for HTTP. esc_url() makes sure it is an URL and not some malicious PHP or SQL. Both functions differ in their target context and their return value in cases of invalid input. – fuxia May 7 '12 at 3:53
  • @toscho Thanks for the info, I just revised the answer a little bit to include esc_url where it seemed appropriate per your suggestion. – Evan Mattson May 8 '12 at 2:13

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.