The question:
The plugin I am working on contains shortcodes with forms on them. Upon form submission (POST), I do some database work, and then planned on redirecting to another page after the db work is complete. When I attempt this, I get the “Headers Already Sent” warning. Within the shortcode function, how do I call my redirect sooner than get_headers? Do I need to use some separate callback function?
The Solutions:
Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.
Method 1
You have to hook an earlier action to check if the form was submitted. Something like:
function wpa_process_form(){
if( isset( $_POST['my_form_data'] ) ){
// process form and redirect
}
}
add_action( 'init', 'wpa_process_form' );
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0