Handle admin form submissions with admin-post.php
A button on a plugin’s admin page that starts an action (sync products, clear a cache, export a report) needs a place to send the form. WordPress has one for exactly this: wp-admin/admin-post.php. A hidden action field decides which hook runs, the handler does the work and redirects back, and the result is shown as a notice. No AJAX, no separate PHP file in the plugin folder.
Problem: A “Sync products now” button on a plugin page must run an import, protect the action against CSRF, and show the result to the administrator afterwards.
Solution: Post the form to admin_url( 'admin-post.php' ) with a hidden action and a nonce field. Handle it on admin_post_{action}: check the nonce and the capability, do the work, redirect back with the result in the URL, and show the notice on the page.
The form
<?php
function ha_render_sync_page() {
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( esc_html__( 'Not allowed.', 'textdomain' ) );
}
$result = isset( $_GET['ha_sync'] ) ? sanitize_key( $_GET['ha_sync'] ) : '';
if ( 'done' === $result ) {
printf( '<div class="notice notice-success is-dismissible"><p>%s</p></div>',
esc_html( sprintf( __( 'Imported %d products.', 'textdomain' ), absint( $_GET['count'] ?? 0 ) ) ) );
} elseif ( 'failed' === $result ) {
echo '<div class="notice notice-error"><p>' . esc_html__( 'The import failed. See the error log.', 'textdomain' ) . '</p></div>';
}
?>
<div class="wrap">
<h1><?php esc_html_e( 'Product sync', 'textdomain' ); ?></h1>
<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>">
<input type="hidden" name="action" value="ha_sync_products">
<?php wp_nonce_field( 'ha_sync_products' ); ?>
<?php submit_button( __( 'Sync products now', 'textdomain' ) ); ?>
</form>
</div>
<?php
}
The handler
<?php
add_action( 'admin_post_ha_sync_products', function () {
check_admin_referer( 'ha_sync_products' ); // dies with "The link you followed has expired" when invalid
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( esc_html__( 'Not allowed.', 'textdomain' ), 403 );
}
$result = ha_run_product_import(); // array( 'count' => 42 ) or WP_Error
$back = admin_url( 'tools.php?page=ha-sync' );
if ( is_wp_error( $result ) ) {
error_log( 'Product sync: ' . $result->get_error_message() );
$back = add_query_arg( 'ha_sync', 'failed', $back );
} else {
$back = add_query_arg( array( 'ha_sync' => 'done', 'count' => (int) $result['count'] ), $back );
}
wp_safe_redirect( $back );
exit;
} );
The hook name is admin_post_ plus the value of the action field. For forms that guests may send (a front-end form), the hook is admin_post_nopriv_{action}; logged-in users always go to admin_post_{action}.
The redirect after the work (the Post/Redirect/Get pattern) means that reloading the result page does not run the import again.
Long-running work
A request that takes minutes hits PHP and proxy time limits, and the administrator stares at a loading page. For big imports, let the handler only schedule the work and return immediately:
<?php wp_schedule_single_event( time(), 'ha_run_product_import_event' );
The notice then says “the import has started”, and the result is shown later, for example from an option the import writes when it is done.
NOTE: Use admin-post.php for real form submissions that end with a redirect, and admin-ajax.php or the REST API for requests from JavaScript that expect data back. Both run the same capability and nonce checks; they differ in what the browser does with the answer.