[ad_1]
@threadi From the doc
Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP.
Normally, we will have another file/script to handle the form like, right?
Assume index.php has a form and it looks like this:
<form action="process.php" method="post"></form>
Then in the process.php file, we use $_POST to get the data like this ( call this “example-1” for later reference ):
$name = $_POST['name'];
header ( "Location: " ); // Redirect user to after processing the data.
But if we put the processing code above on the same file ( index.php ) and it looks like this ( call this “example-2” for later reference ):
<form action="process.php" method="post"></form>
<?php
$name = $_POST['name'];
header ( "Location: " );
?>
It would cause an error because there is already an output ( the form ) before the header ().
Now let’s get back to the “WordPress” way of handling form, we will use the hook admin_post_{$action} with a function like:
add_action( admin_post_handle_data, handle_data );
Then we write the processing code inside handle_data like the below:
function handle_data () {
$name = $_POST['name'];
header ( "Location: " );
}
The form page is created by add_menu_page with the admin_menu hook.
My question is:
1) Will this function handle_data () be include/require on the form page? If it does, it will look like the “example-2”.
2) If the question “1)” is a yes, then the sequence matter, whether it’s admin_post first or admin_menu first. If it’s admin_post first, then the function handle_data () is before the form; If it’s the admin_menu first, then the function handle_data () is after the form.
Or am I complicating it, WordPress will just do the traditional way like “example-1” where the processing code is in another script/file?
