Skip to content

Instantly share code, notes, and snippets.

@mathewjosephh
Created October 27, 2016 12:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mathewjosephh/1a4c9eae15b7987a2b78b86047701369 to your computer and use it in GitHub Desktop.
Save mathewjosephh/1a4c9eae15b7987a2b78b86047701369 to your computer and use it in GitHub Desktop.
Wordpress Create custom fields and send mail
add_action('post_submitbox_misc_actions', 'send_to_user_block');
function send_to_user_block()
{
global $pagenow;
$post_id = get_the_ID();
if (get_post_type($post_id) != 'post') {
return;
}
if (in_array( $pagenow, array( 'post.php' ) )){
$userNameValue = get_post_meta($post_id, 'user_name_field', true);
$userEmailValue = get_post_meta($post_id, 'user_email_field', true);
wp_nonce_field('my_custom_nonce_'.$post_id, 'my_custom_nonce');
echo '<div id="send-to-user-block" class="misc-pub-section" style="border-top-style:solid; border-top-width:1px; border-top-color:#EEEEEE; border-bottom-width:0px;">
<input type="text" name="user_name_field" placeholder="Name" id="user_name_field" value="">
<input type="text" name="user_email_field" placeholder="Email" id="user_email_field" value="">
<input id="send_mail" class="button button-highlighted" type="submit" value="Send Mail" name="send_mail">
</div>';
}
}
add_action('save_post', 'sendMail');
function sendMail( $post_id )
{
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return;
}
if (!isset($_POST['my_custom_nonce']) || !wp_verify_nonce($_POST['my_custom_nonce'], 'my_custom_nonce_'.$post_id)) {
return;
}
if (!current_user_can('edit_post', $post_id)) {
return;
}
if (isset($_POST['user_name_field']) && isset($_POST['user_email_field']) && isset($_POST['send_mail']) && $_POST['send_mail'] == 'Send Mail') {
if(get_permalink($post_id) && sanitize_email($_POST['user_email_field'])) {
$name = sanitize_text_field($_POST['user_name_field']);
$email = sanitize_email($_POST['user_email_field']);
$msg = 'Hi '.$name.',<br> <a href="'.get_permalink($post_id).'">View Your Story</a>';
mail($email,"Your Story",$msg);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment