For some reason wp_redirect/wp_safe_redirect in WordPress can be a total pain, whether it be when writing custom front end code, plugins or widgets I seem to come across an issue with redirects periodically, with the utterly frustrating “Cannot modify header information – headers already sent” error. As a heads up, this is caused as the name suggests by content in a header being pushed down before the redirect is called, this is a generic issue not one that is specific to WordPress. More often than not it is simple to identify, if any of the involved PHP files have leading or trailing white space, and or there’s some hidden debug output you have forgotten. Occasionally the issue is WordPress however, in my case the issue of wp_redirect not working on a custom admin page, was caused by a function in
/wp-admin/includes/template.php:1636 or so, whereby headers/html, quite a lot of them are indeed sent as part of the page load process.
Having ensured all of my files were trimmed and there was nothing wrong with my plugin code, I discovered here that there’s a little workaround for forms being called in custom admin pages, that is simply to append:
&noheader=true
To your action, e.g.
<form method=”POST” action=”?page=your-plugin-page&noheader=true”>
This resolved my problem instantly – understandably given it suppressed the content being produced in template.php!
Hope it helps anyone having problems with wp_redirect and wp_safe_redirect on custom admin pages in WordPress.
