Custom form action hook

admin_url( 'admin-post.php' ) is the non-ajax version of admin_url( 'admin-ajax.php' )

Similar to admin-ajax.php it will fire one of four hooks:

Logged out users

  • admin_post_nopriv_{action} ($_REQUEST['action'] specified)
  • admin_post_nopriv (no $_REQUEST['action'] specified)

Logged in user

  • admin_post_{action} ($_REQUEST['action'] specified)
  • admin_post (no $_REQUEST['action'] specified)

The ‘action’ can therefer bespecified as part of the target url (e.g. admin_url( 'admin-ajax.php?action=my-action' )) or as posted along with all the other data.

The disadvantage to this is that if you need to return the user to the form (e.g. because of an error), you will need to redirect them, and so the browser will loose the data they entered. That’s terrible UX, so I rarely use this.

Leave a Comment