Long-time reader of answers, first-time asker of questions...
I have encountered a problem that I cannot debug involving a custom function (defined within the theme's functions.php) that is executed as a form action upon submiting data from a basic HTML form element located in a page template file.
The function gathers the form data (and a few other bits of info from meta fields and theme mods), defines it for use with wp_mail, which is then executed to send an email as defined, the function then executes a redirect to take the user to a thank you page.
Here's the situation: during the localhost portion of development (running WampServer Version 2.2) my function seemed to work just fine, gathering form data and sending the user to correct destination as specified by the wp_redirect location. The email never actually left my virtual server, as I have not configured it for SMTP, so I transferred the theme to a test server running the same version of WordPress to finish beta testing. Now that it is on a real server, the wp_mail side of the function is successfully formatting and sending the email, but rather than loading the redirect location, the function as executed is reloading the source page minus the form element.
Here is a link to the page in question on my test server.
I have tried troubleshooting this problem in the following ways:
1) I have completely removed the wp_mail portion of the function to isolate the wp_redirect portion. The isolated redirect doesn't execute correctly on the live test server, but does on my localhost test server.
2) I have tried defining the location of that redirect in many different ways, including a hard-coded URL, a call to get post meta via the post ID, and then by incorporating the data into the form element as a hidden input, and using the same $form_data method of passing on the field data that is successfully implemented in the mail portion of my function.
3) I have tried manually exiting out of the wp_mail function before calling wp_redirect and have tried removing the suggested manual exit for wp_redirect. When running the function with no exits, the source page reloads on form submission, WITH the form element reloading as normal - but if there is an exit after wp_mail or wp_redirect, the source page reloads WITHOUT the form element.
4) I have tried re-positioning my call to wp_redirect within the function to before the wp_mail reference as well as outside of the if statement.
5) I have checked to make sure that my online server (hosted through GoDaddy) is running the same version of PHP, 5.3, and have even looked at my htaccess file for possible conflicts with the redirect.
The main thing about this problem that confuses me is that the redirect works just fine on my local server in every permutation described above but just plain won't work online. I hope there is an obvious error in my methodology that will lead to a more thorough understanding of core WP functionality - it would hurt my pride, but I want this to be a rookie mistake.
So I think that's the whole scope of the problem, any help identifying the source of this problem would be greatly appreciated - thank you in advance for your attention and support.
This is my custom function:
function mwp_mail() {
// send to admin email
$email = get_theme_mod('mwp_mail_to_address', get_bloginfo( 'admin_email' ));
// if the <form> element is POSTed, run the following code
if ( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
// this part fetches everything that has been POSTed, sanitizes them and lets us use them as $form_data['subject']
foreach ( $_POST as $field => $value ) {
$form_data[$field] = sanitize_text_field( $value );
}
// get the website's name and puts it in front of the subject
$email_subject = get_bloginfo( 'name' ) . " - " . $form_data['page'] . " - CTA lead";
// get the message from the form and add the IP address of the user below it
$email_message = $form_data['your_name'] . " is interested in your service. Contact them at: " . $form_data['email'] . "\n\nIP: " . get_the_ip();
// send the e-mail with the post form attribute named 'email' and the POSTed data
wp_mail( $email, $email_subject, $email_message );
wp_redirect ($form_data['redirect']);
exit;
}
}
This is my form markup:
<form name="ctaForm" action="<?php mwp_mail(); ?>" method="post" onsubmit="return validate()">
<input type="hidden" name="page" id="page" value="<?php echo get_the_title(); ?>"/>
<input type="hidden" name="redirect" id="redirect" value="<?php echo get_post_meta( get_the_ID(), 'mwp_cta_thankyou', true ); ?>"/>
<input type="text" id="your_name" name="your_name" value="give us your name"/>
<input type="text" id="email" name="email" value="and email address"/>
<input type="submit" value="<?php echo get_post_meta( get_the_ID(), 'mwp_cta_button', true ); ?>" class="CTAbutton"/>
<input type="button" id="closeCTA" value="X" class="CTAbutton"/>
<a href="<?php echo get_post_meta( get_the_ID(), 'mwp_cta_pplink', true ); ?>" target="new" class="privacyLink">Privacy Policy</a>
</form>
And for good measure, my htaccess file:
#Alternate default index page
DirectoryIndex welcome.html index.php index.html
# Custom 404 errors
ErrorDocument 404 /404.html
# BEGIN WordPress
# END WordPress
Thanks for reading. Hope you can help me!