do_action( "admin_post_{$action}" )

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


Description Description

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


Source Source

File: wp-admin/admin-post.php

View on Trac


Top ↑

Changelog Changelog

Changelog
Version Description
2.6.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note content
    Contributed by Aamer Shahzad

    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}

    add_action( 'admin_post_our_action_hook', 'am_our_action_hook_function' );
    function am_our_action_hook_function() {
        // do something
    }
  2. Skip to note content
    Contributed by Aamer Shahzad
    &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" />
    &lt;/form>
    /**
     * 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( 
    			$value['ID'],
    			$value['post_title'],
    			$value['post_date']
    		);
    
    		fputcsv( $output, $modified_values );
    	}
    	return $output;
    }

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