Skip to content

Instantly share code, notes, and snippets.

@kingjmaningo
Last active January 9, 2024 13:44
Show Gist options
  • Save kingjmaningo/2b6ac32cf43376c57da2b17cc820fbbb to your computer and use it in GitHub Desktop.
Save kingjmaningo/2b6ac32cf43376c57da2b17cc820fbbb to your computer and use it in GitHub Desktop.
Custom form submission using admin-post with wp_mail()
<!-- Sample custom form-->
<!-- We use admin-post.php provided to fully utilize the event driven nature of WordPress. -->
<form id="form-id" action="<?php echo esc_url( admin_url('admin-post.php') ); ?>" method="post">
<label for="first-name">First Name</label>
<input type="text" name="first-name" id="first-name" required>
<label for="last-name">Last Name</label>
<input type="text" name="last-name" id="last-name" required>
<label for="email">Email Address</label>
<input type="email" name="email" id="email" required>
<label for="birthday">Birthday:</label>
<input type="date" id="birthday" name="birthday">
<!-- We also need to add the action hidden input so that we can trigger the more specific hook -->
<input type="hidden" name="action" value="form_submit_action">
<input type="submit" class"submit-form" value="Submit"/>
</form>
<?php
// In your functions.php
// Action function
function form_submit_action() {
// You can now use $_GET/$_POST variables depending on what method you used in your form
// In this case we are using method post
$first_name = sanitize_text_field($_POST['first-name']);
$last_name = sanitize_text_field($_POST['last-name']);
$email = sanitize_email($_POST['email']);
$birthday = sanitize_text_field($_POST['birthday']);
// Then do the processing here like create new post/user, update new post/user , etc.
// But on this example im gonna show you how send an email, create your own custom html body format.
// Send to admin
$to = get_bloginfo('admin_email'); // or 'sendee@email.com' to specify email
// Email subject
$subject = 'Customer Wishlist';
// Email body/content (tricky part)
/* Instead of:
$body = '<div>
<p>'. $first_name .'</p>
</div>';
*/
// We can create a custom function with the post fields as your attributes
$body = my_email_body_function($first_name,$last_name,$email,$birthday);
$headers = array('Content-Type: text/html; charset=UTF-8');
wp_mail( $to, $subject, $body, $headers );
// Then redirect to desired page
wp_redirect(home_url('/desired-page'));
}
// Necessary action hooks
// Use our specific action form_submit_action to process the data related to our request
add_action( 'admin_post_nopriv_form_submit_action', 'form_submit_action' );
add_action( 'admin_post_form_submit_action', 'form_submit_action' );
// Email body function declaration
function my_email_body_function($first_name,$last_name,$email,$birthday) {
ob_start(); // We have to turn on output buffering. VERY IMPORTANT! or else wp_mail() wont work
// Then setup your email body using the postfields from the attritbutes passed on. ?>
<table style="width:100%; border-collapse: collapse;">
<tr>
<th style="border: 1px solid #dddddd;text-align: left;padding: 8px;">Name:</th>
<th style="border: 1px solid #dddddd;text-align: left;padding: 8px;"><?php echo $first_name; ?></th>
</tr>
<tr>
<th style="border: 1px solid #dddddd;text-align: left;padding: 8px;">Last Name:</th>
<th style="border: 1px solid #dddddd;text-align: left;padding: 8px;"><?php echo $last_name; ?></th>
</tr>
<tr>
<th style="border: 1px solid #dddddd;text-align: left;padding: 8px;">Email:</th>
<th style="border: 1px solid #dddddd;text-align: left;padding: 8px;"><?php echo $email; ?></th>
</tr>
<tr>
<th style="border: 1px solid #dddddd;text-align: left;padding: 8px;">Birthday:</th>
<th style="border: 1px solid #dddddd;text-align: left;padding: 8px;"><?php echo $birthday; ?></th>
</tr>
</table
<?php
return ob_get_contents();
ob_get_clean();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment