handle post requests wordpress code example

Example: post form submission wordpress

// The Form:
<form action="<?php echo esc_url( admin_url('admin-post.php') ); ?>" method="post">

<input type="hidden" name="action" value="contact_form">

// The Plugin or functions.php
function prefix_send_email_to_admin() {
    /**
     * At this point, $_GET/$_POST variable are available
     *
     * We can do our normal processing here
     */ 

    // Sanitize the POST field
    // Generate email content
    // Send to appropriate email
}
add_action( 'admin_post_nopriv_contact_form', 'prefix_send_email_to_admin` );
add_action( 'admin_post_contact_form', 'prefix_send_email_to_admin` );

Tags:

Php Example