This hook allows you to create custom handlers for your own custom GET and POST requests. The admin_post_ hook follows the format “admin_post_$action“, where $action is your GET or POST request’s ‘action‘ parameter.
Usage:
If you needed to create a request or form handler for an “add_foobar” action request, you would create a hook like this:
add_action( 'admin_post_add_foobar', 'prefix_admin_add_foobar' );
function prefix_admin_add_foobar() {
// Handle request then generate response using echo or leaving PHP and using HTML
}
Using the above example, any time a GET or POST request is sent to WordPress, and the request’s ‘action‘ parameter is set to ‘add_foobar‘, this hook will be automatically executed. For example, the following HTML content would execute the above hook when the user clicks either Submit.
Note: The data value (foobarid) would be available in your hook function from the $_GET, $_POST or $_REQUEST array as is applicable.
Example:
This following example allows you to hook the GET or POST requests from the above html.
add_action( 'admin_post_add_foobar', 'prefix_admin_add_foobar' );
//this next action version allows users not logged in to submit requests
//if you want to have both logged in and not logged in users submitting, you have to add both actions!
add_action( 'admin_post_nopriv_add_foobar', 'prefix_admin_add_foobar' );
function prefix_admin_add_foobar() {
status_header(200);
//request handlers should exit() when they complete their task
exit("Server received '{$_REQUEST['data']}' from your browser.");
}
This hook allows us to create our own handler for GET or POST request. for example we want to call a function when form is submitted, we can do this with the following code.
This hook allows us to create our own handler for GET or POST request. for example we want to call a function when form is submitted, we can do this with the following code.
<form action="http://www.example.com/wp-admin/admin-post.php" method="post">
<input type="hidden" name="action" value="our_action_hook">
<input type="submit" value="Submit">
</form>
This will submit form on the following url
http://www.example.com/wp-admin/admin-post.php?action=our_action_hook
in our function.php we can call the hook as “admin_post_” followed by {$action}
You can not submit a form from post edit page in admin area.
You can add a button with url to admin-post.php and pass arg as http query like :
Then in your admin_init add action like: