WildBoiBill Posted September 12, 2014 Share Posted September 12, 2014 I was able to edit this code from my word press theme.. I wanted to add a social club ID to as a text field, i was able to add the text field but i cannot seem to get the information from that field sent with the email I edited this contact form file on my word press to try and include a Social ID field, i was able to add the content to the form but i'm unable to get the information sent in the email, keeps giving me awkward results. help plz. <?php session_start(); /** * Template Name: Contact Form */ global $theme; get_header(); ?> <div id="main"> <?php $theme->hook('main_before'); ?> <div id="content"> <?php $theme->hook('content_before'); ?> <?php if (have_posts()) : while (have_posts()) : the_post(); /** * Find the post formatting for the pages in the post-page.php file */ get_template_part('post', 'page'); endwhile; else : get_template_part('post', 'noresults'); endif; ?> <div class="contact-form"> <?php if ($_POST['contact_form_submit'] ) { if(!$_POST['contact_form_name'] || !$_POST['contact_form_email'] || !$_POST['contact_form_subject'] || !$_POST['contact_form_socialid'] || !$_POST['contact_form_question'] || !$_POST['contact_form_message']) { ?><div class="error"><?php _e('Please fill in all required fields!','themater'); ?></div><?php } elseif(!is_email($_POST['contact_form_email'])) { ?><div class="error"><?php _e('Invalid email!','themater'); ?></div><?php } elseif(($_SESSION['contact_form_number_one'] + $_SESSION['contact_form_number_two']) != $_POST['contact_form_question']) { ?><div class="error"><?php _e('You entered the wrong solution..','themater'); ?></div><?php } else { wp_mail($theme->get_option('contact_form_email'), sprintf( '[%s] ' . esc_html($_POST['contact_form_subject']), get_bloginfo('name') ), esc_html($_POST['contact_form_message']),'From: "'. esc_html($_POST['contact_form_name']) .'" <' . esc_html($_POST['contact_form_email']) . '>'); unset($_POST); ?><div class="message"><?php _e('Thanks for contacting Devils First MC.','themater'); ?></div><?php } if ( isset($_SESSION['contact_form_number_one'] ) ) unset( $_SESSION['contact_form_number_one'] ); if ( isset($_SESSION['contact_form_number_two'] ) ) unset( $_SESSION['contact_form_number_two'] ); } if ( !isset($_SESSION['contact_form_number_one'] ) ) $_SESSION['contact_form_number_one'] = $contact_form_number_one = rand(1, 9); else $contact_form_number_one = $_SESSION['contact_form_number_one']; if ( !isset($_SESSION['contact_form_number_two'] ) ) $_SESSION['contact_form_number_two'] = $contact_form_number_two = rand(1, 9); else $contact_form_number_two = $_SESSION['contact_form_number_two']; ?> <form method="post" action=""> <input type="hidden" name="contact_form_submit" value="true" /> <div class="contact-form-label alignleft <?php if($_POST && !$_POST['contact_form_name']) { echo 'contact-form-required'; } ?>"><?php _e('Name','themater'); ?>:</div> <div class="contact-form-input"><input type="text" name="contact_form_name" value="<?php if ( isset($_POST['contact_form_name']) ) { echo esc_attr($_POST['contact_form_name']); } ?>" /></div> <div class="contact-form-label alignleft <?php if($_POST && !$_POST['contact_form_socialid']) { echo 'contact-form-required'; } ?>"><?php _e('Social Club ID','themater'); ?>:</div> <div class="contact-form-input"><input type="text" name="contact_form_socialid" value="<?php if ( isset($_POST['contact_form_socialid']) ) { echo esc_attr($_POST['contact_form_socialid']); } ?>" /></div> <div class="contact-form-label alignleft <?php if($_POST && !$_POST['contact_form_email']) { echo 'contact-form-required'; } ?>"><?php _e('Email','themater'); ?>:</div> <div class="contact-form-input"><input type="text" name="contact_form_email" value="<?php if ( isset($_POST['contact_form_email']) ) { echo esc_attr($_POST['contact_form_email']); } ?>" /></div> <div class="contact-form-label alignleft <?php if($_POST && !$_POST['contact_form_question']) { echo 'contact-form-required'; } ?>"><?php echo $contact_form_number_one; ?> + <?php echo $contact_form_number_two; ?> = ?</div> <div class="contact-form-input"><input type="text" name="contact_form_question" value="" /></div> <div class="contact-form-label alignleft <?php if($_POST && !$_POST['contact_form_subject']) { echo 'contact-form-required'; } ?>"><?php _e('Subject','themater'); ?>:</div> <div class="contact-form-input"><input type="text" name="contact_form_subject" value="<?php if ( isset($_POST['contact_form_subject']) ) { echo esc_attr($_POST['contact_form_subject']); } ?>" /></div> <div class="contact-form-label alignleft <?php if($_POST && !$_POST['contact_form_message']) { echo 'contact-form-required'; } ?>"><?php _e('Message','themater'); ?>:</div> <div class="contact-form-input"><textarea name="contact_form_message" ><?php if ( isset($_POST['contact_form_message']) ) { echo esc_textarea($_POST['contact_form_message']); } ?></textarea></div> <div class="contact-form-label alignleft"> </div> <div class="contact-form-input" style="text-align: center;"><input type="submit" value="<?php _e('Submit','themater'); ?>" /></div> </form> </div> <?php $theme->hook('content_after'); ?> </div><!-- #content --> <?php get_sidebars(); ?> <?php $theme->hook('main_after'); ?> </div><!-- #main --> <?php get_footer(); ?> Quote Link to comment Share on other sites More sharing options...
gizmola Posted September 12, 2014 Share Posted September 12, 2014 I'm not sure what "awkward results" is supposed to mean. I'm interpreting your questions as --- "it doesn't appear in the contact email the script sends to me". This script is "self-posting" in that it has 2 parts: 1. If it was the target of a POST request, then it looks at the contents of the $_POST and if acceptable, sends the email. 2. If it was not POSTED to OR there was an error, display the html form. You added a form element to section 2 of the code. You did not make any changes to Section1, so even if the new form field is filled out, it's simply discarded. Section 1 of the code starts with this logical condition: if ($_POST['contact_form_submit'] ) { Hopefully you understand that this is checking to see if the $_POST super global includes the submit button. This will happen when the form is submitted. You might be wondering why the script submits to itself? It's because a specific target is not included in the form tag using the action= parameter. When this happens the browser assumes that the target should be the same URL as the form. <form method="post" action=""> In summary, you need to look at the code that comes directly after the 'if' statement I provided, and figure out where to account for the existence of the new form variable you created. Since it's a "required" field in your form, you actually need to change both the code that sends the email as well as the code that comes before it, which checks for the existence of all the required fields on the form. Needless to say that variable will be named: $_POST['contact_form_socialid'] Quote Link to comment Share on other sites More sharing options...
WildBoiBill Posted September 12, 2014 Author Share Posted September 12, 2014 (edited) I updated the code that i believe to send the email, and the result was not what i wanted to see: Original Code: wp_mail($theme->get_option('contact_form_email'), sprintf( '[%s] ' . esc_html($_POST['contact_form_subject']), get_bloginfo('name') ), esc_html($_POST['contact_form_message']),'From: "'. esc_html($_POST['contact_form_name']) .'" <' . esc_html($_POST['contact_form_email']) . '>'); The results were normal as showed My Edit: wp_mail($theme->get_option('contact_form_email'), sprintf( '[%s] ' . esc_html($_POST['contact_form_subject']), get_bloginfo('name') ), esc_html($_POST['contact_form_message']), esc_html($_POST['contact_form_socialid']),'From: "'. esc_html($_POST['contact_form_name']) .'" <' . esc_html($_POST['contact_form_email']) . '>'); The results where "different": the name of the sender was shown as WordPress and the email was shown as my own, the social id was displayed, but was displayed where the message was and the message is not being displayed.... any help with this part? Edited September 12, 2014 by WildBoiBill Quote Link to comment Share on other sites More sharing options...
WildBoiBill Posted September 12, 2014 Author Share Posted September 12, 2014 I actually figured it out.... i got it to show correctly in the email... now i just want them to display differently My First Edit: wp_mail($theme->get_option('contact_form_email'), sprintf( '[%s] ' . esc_html($_POST['contact_form_subject']), get_bloginfo('name') ), esc_html($_POST['contact_form_message']), esc_html($_POST['contact_form_socialid']),'From: "'. esc_html($_POST['contact_form_name']) .'" <' . esc_html($_POST['contact_form_email']) . '>'); My Fix: wp_mail($theme->get_option('contact_form_email'), sprintf( '[%s] ' . esc_html($_POST['contact_form_subject']), get_bloginfo('name') ), esc_html($_POST['contact_form_message']) . esc_html($_POST['contact_form_socialid']),'From: "'. esc_html($_POST['contact_form_name']) .'" <' . esc_html($_POST['contact_form_email']) . '>'); It's sending all the correct information but it's displaying together in the email, how can i get it like this: How it is now: SOCIAL IDMESSAGE How I want it: Social ID: "the input" Message: "the message" Does that make sense?? Quote Link to comment Share on other sites More sharing options...
WildBoiBill Posted September 12, 2014 Author Share Posted September 12, 2014 I got it to display the way i want it in the email but now how do i had a "break" My Fix: wp_mail($theme->get_option('contact_form_email'), sprintf( '[%s] ' . esc_html($_POST['contact_form_subject']), get_bloginfo('name') ), 'Social ID: ' . esc_html($_POST['contact_form_socialid']) . 'Message:' . esc_html($_POST['contact_form_message']),'From: "'. esc_html($_POST['contact_form_name']) .'" <' . esc_html($_POST['contact_form_email']) . '>'); It displays like this: Social ID:"input"Message:"input" how do i get a line break in between them? to get this: Social ID: "input" Message: "input" Quote Link to comment Share on other sites More sharing options...
WildBoiBill Posted September 12, 2014 Author Share Posted September 12, 2014 (edited) well.... i'm sorry i did this but maybe someone can use this in the future... but i figured that out as well.... when using single quotes ('') you can just add a line when using double quotes ("") you add /n My Fix: wp_mail($theme->get_option('contact_form_email'), sprintf( '[%s] ' . esc_html($_POST['contact_form_subject']), get_bloginfo('name') ), 'Social ID: ' . esc_html($_POST['contact_form_socialid']) . 'added a 'enter button' break in the line here Message:' . esc_html($_POST['contact_form_message']),'From: "'. esc_html($_POST['contact_form_name']) .'" <' . esc_html($_POST['contact_form_email']) . '>'); Edited September 12, 2014 by WildBoiBill Quote Link to comment Share on other sites More sharing options...
gizmola Posted September 12, 2014 Share Posted September 12, 2014 Congrats on making the effort to figure this out on our own. Posting your final solution is good form as well, and helps give back to the community. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.