do_action( “admin_post_{$action}” )

Fires on an authenticated admin post request for the given action.


The dynamic portion of the hook name, $action, refers to the given request action.

More Information

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.


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.

<a href="">Submit</a>

<form action="" method="post">
<input type="hidden" name="action" value="add_foobar">
<input type="hidden" name="data" value="foobarid">
<input type="submit" value="Submit">

Note: The data value (foobarid) would be available in your hook function from the $_GET, $_POST or $_REQUEST array as is applicable.


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() {
//request handlers should exit() when they complete their task
exit("Server received '{$_REQUEST['data']}' from your browser.");


do_action( "admin_post_{$action}" );



User Contributed Notes

  1. Skip to note 4 content
    &lt;form action="<?php echo admin_url( 'admin-post.php' ); ?>">
    	&lt;input type="hidden" name="action" value="generate_csv" />
    	&lt;input type="submit" name="submit" class="button button-primary" value="Generate & Download CSV File" />
     * Generate CSV File.
    add_action( 'admin_post_generate_csv', 'lunchbox_generate_orders_csv' );
    function lunchbox_generate_orders_csv() {
    	global $wpdb;
    	$filename = 'lunchbox-orders';
    	$generatedDate = $generatedDate = date('d-m-Y His');
    	 * output header so that file is downloaded
    	 * instead of open for reading.
    	header("Pragma: public");
    	header("Expires: 0");
    	header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    	header("Cache-Control: private", false);
    	header('Content-Type: text/csv; charset=utf-8');
    	// header("Content-Type: application/octet-stream");
    	header("Content-Disposition: attachment; filename=\"" . $filename . " " . $generatedDate . ".csv\";" );
    	// header('Content-Disposition: attachment; filename=lunchbox_orders.csv');
    	header("Content-Transfer-Encoding: binary");
    	 * create a file pointer connected to the output stream
    	 * @var [type]
    	$output = fopen('php://output', 'w');
    	$results = $wpdb->get_results( "SELECT * FROM $wpdb->posts WHERE post_type = 'shop_order'", ARRAY_A );
    	 * output the column headings
    	fputcsv( $output, array('Order ID', 'Order Title', 'Order Date'));
    	foreach ( $results as $key => $value ) {
    		// $array[] = '';
    		$modified_values = array( 
    		fputcsv( $output, $modified_values );
    	return $output;
  2. Skip to note 5 content

    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="" method="post">
    <input type="hidden" name="action" value="our_action_hook">
    <input type="submit" value="Submit">

    This will submit form on the following url

    in our function.php we can call the hook as “admin_post_” followed by {$action}

    add_action( 'admin_post_our_action_hook', 'am_our_action_hook_function' );
    function am_our_action_hook_function() {
        // do something
  3. Skip to note 6 content

    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 :

    $my_form_nonce = wp_create_nonce( 'my_form_nonce' );
    $get_data = http_build_query( array( 
        'my_form_nonce' => $my_form_nonce, 
        'action' => 'my_form_response', 
        'post_id' => $post->ID, 
    ) );
    $url = admin_url( 'admin-post.php' ) . '?' . $get_data;
    add an a tag with the href="$url". 

    Then in your admin_init add action like:

    add_action( 'admin_post_my_form_response', 'wpdocs_form_callback' );
    // callback function 
    function wpdocs_form_callback() {
    	// can access &_GET
    	// can check nonce 
    	// $post_id = $_GET['post_id'];

You must log in before being able to contribute a note or feedback.