Sequence of admin_post and admin_menu

@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>

$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?

  • This reply was modified 58 minutes ago by ace0930.


This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer