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 itthe 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 = $_POST['redirect'];
and added this right after the option is updated:
if esc_url_raw($redirect) {$_POST['redirect'] );
wp_redirect( $redirect );
exit;
}
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.