email field to point to an email form
-
This is a fascinating plugin. I’m exploring it to see if I can get it list practitioners info with an email field that is a link to a generic email form that hides the practitioners email from the end user. In other words They can send the practitioner a message through the site directly without being disclosed said practitioner’s email address.
-
Well, you first have to create the email form functionality. There may be a plugin out there that does this, but if you are fairly familiar with PHP and WordPress coding it’s not too hard to roll your own with something like that.
Then, you’d have to create a custom template for the list shortcode that takes whatever field you want to use as your “send email” link and turns it into whatever you need the URL to be to set up your email form to send to the right address.
Thank you! I will dig deeper.
Quick question: how do I call the email form (below) from the custom template passing to it the appropriate hidden email that has been chosen by the user?
<?php /* * Template Name: IABT-Contact-form * This is the generic form that will be used to email practitioners * copied from https://www.catswhocode.com/blog/how-to-create-a-built-in-contact-form-for-your-wordpress-theme */ ?> <?php if(isset($_POST['submitted'])) { if(trim($_POST['contactName']) === '') { $nameError = 'Please enter your name.'; $hasError = true; } else { $name = trim($_POST['contactName']); } if(trim($_POST['comments']) === '') { $commentError = 'Please enter a message.'; $hasError = true; } else { if(function_exists('stripslashes')) { $comments = stripslashes(trim($_POST['comments'])); } else { $comments = trim($_POST['comments']); } } if(!isset($hasError)) { $emailTo = get_option('tz_email'); if (!isset($emailTo) || ($emailTo == '') ){ $emailTo = get_option('admin_email'); } // $subject = '[' . get_bloginfo('name') . ' Contact Form] From '.$name; $subject = '[Foo.org Contact Form] From '.$name; $body = "Name: $name \n\nEmail: $email \n\nComments: $comments"; $headers = 'From: '.$name.' <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email; wp_mail($emailTo, $subject, $body, $headers); $emailSent = true; } } ?> <?php get_header(); ?> <div id="container"> <div id="content"> <?php if (have_posts()) : while (have_posts()) : the_post(); ?> <div <?php post_class() ?> id="post-<?php the_ID(); ?>"> <h1 class="entry-title"><?php the_title(); ?></h1> <div class="entry-content"> <?php if(isset($emailSent) && $emailSent == true) { ?> <div class="thanks"> <p>Thanks, your email was sent successfully.</p> </div> <?php } else { ?> <?php the_content(); ?> <?php if(isset($hasError) || isset($captchaError)) { ?> <p class="error">Sorry, an error occured.<p> <?php } ?> <form action="<?php the_permalink(); ?>" id="contactForm" method="post"> <ul class="contactform"> <li> <label for="contactName">Name:</label> <input type="text" name="contactName" id="contactName" value="<?php if(isset($_POST['contactName'])) echo $_POST['contactName'];?>" class="required requiredField" /> <?php if($nameError != '') { ?> <span class="error"><?=$nameError;?></span> <?php } ?> </li> <li> <label for="email">Email:</label> <input type="text" name="email" id="email" value="hidden" class="practitioner-email" readonly /> </li> <li><label for="commentsText">Message:</label> <textarea name="comments" id="commentsText" rows="20" cols="30" class="required requiredField"><?php if(isset($_POST['comments'])) { if(function_exists('stripslashes')) { echo stripslashes($_POST['comments']); } else { echo $_POST['comments']; } } ?></textarea> <?php if($commentError != '') { ?> <span class="error"><?=$commentError;?></span> <?php } ?> </li> <li> <input type="submit">Send email</input> </li> </ul> <input type="hidden" name="submitted" id="submitted" value="true" /> </form> <?php } ?> </div><!-- .entry-content --> </div><!-- .post --> <?php endwhile; endif; ?> </div><!-- #content --> </div><!-- #container --> <?php get_footer(); ?>
Quick question: how do I call the email form (below) from the custom template passing to it the appropriate hidden email that has been chosen by the user?
<?php /* * Template Name: IABT-Contact-form * This is the generic form that will be used to email practitioners * copied from https://www.catswhocode.com/blog/how-to-create-a-built-in-contact-form-for-your-wordpress-theme */ ?> <?php if(isset($_POST['submitted'])) { if(trim($_POST['contactName']) === '') { $nameError = 'Please enter your name.'; $hasError = true; } else { $name = trim($_POST['contactName']); } if(trim($_POST['comments']) === '') { $commentError = 'Please enter a message.'; $hasError = true; } else { if(function_exists('stripslashes')) { $comments = stripslashes(trim($_POST['comments'])); } else { $comments = trim($_POST['comments']); } } if(!isset($hasError)) { $emailTo = get_option('tz_email'); if (!isset($emailTo) || ($emailTo == '') ){ $emailTo = get_option('admin_email'); } // $subject = '[' . get_bloginfo('name') . ' Contact Form] From '.$name; $subject = '[Foo.org Contact Form] From '.$name; $body = "Name: $name \n\nEmail: $email \n\nComments: $comments"; $headers = 'From: '.$name.' <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email; wp_mail($emailTo, $subject, $body, $headers); $emailSent = true; } } ?> <?php get_header(); ?> <div id="container"> <div id="content"> <?php if (have_posts()) : while (have_posts()) : the_post(); ?> <div <?php post_class() ?> id="post-<?php the_ID(); ?>"> <h1 class="entry-title"><?php the_title(); ?></h1> <div class="entry-content"> <?php if(isset($emailSent) && $emailSent == true) { ?> <div class="thanks"> <p>Thanks, your email was sent successfully.</p> </div> <?php } else { ?> <?php the_content(); ?> <?php if(isset($hasError) || isset($captchaError)) { ?> <p class="error">Sorry, an error occured.<p> <?php } ?> <form action="<?php the_permalink(); ?>" id="contactForm" method="post"> <ul class="contactform"> <li> <label for="contactName">Name:</label> <input type="text" name="contactName" id="contactName" value="<?php if(isset($_POST['contactName'])) echo $_POST['contactName'];?>" class="required requiredField" /> <?php if($nameError != '') { ?> <span class="error"><?=$nameError;?></span> <?php } ?> </li> <li> <label for="email">Email:</label> <input type="text" name="email" id="email" value="hidden" class="practitioner-email" readonly /> </li> <li><label for="commentsText">Message:</label> <textarea name="comments" id="commentsText" rows="20" cols="30" class="required requiredField"><?php if(isset($_POST['comments'])) { if(function_exists('stripslashes')) { echo stripslashes($_POST['comments']); } else { echo $_POST['comments']; } } ?></textarea> <?php if($commentError != '') { ?> <span class="error"><?=$commentError;?></span> <?php } ?> </li> <li> <input type="submit">Send email</input> </li> </ul> <input type="hidden" name="submitted" id="submitted" value="true" /> </form> <?php } ?> </div><!-- .entry-content --> </div><!-- .post --> <?php endwhile; endif; ?> </div><!-- #content --> </div><!-- #container --> <?php get_footer(); ?>
Well, you have to use a “GET” instead of “POST” that is, the values must be in the URL… something like
email-form-page/?contactname=name
Thank you. Quick follow-up:
- Won’t that be visible to the user?
- Can spammers use it to send email?
BTW, sorry for the duplicate post. Was having connectivity issues.
Yes on both counts. If you want better security, I suggest you use a SESSION value instead of post or get. It’s a bit more complicated, but a lot harder to spoof.
Xnau,
So if you recall, once the user searches by city/state/country the returned list will have a generic link in the place of the email field. This links to a form which will be populated by the user so they can send an email through the site to the practitioner. The complexity here is hiding the email address. The form will have the interested party populate their name, email address, and personal message. The practitioner email will automagically be added when the ‘send’ button is activated.
As a proof of concept, I’m going to use “GET” as you suggested. How do I pass the arguments to the email-form-page. Also does the email form page need to be a wordpress page (or a wordpress template?)
So here is the workflow:
widget contains [pdb-list] – >
Returns list of field including email field that is a generic link ->
when link is pressed it passes the appropriate fields to the email-form-page ->
Email form page accepts input from user and does some voodoo when ‘Send’ button is pressed to deliver mail to the target participantOK, it’s up to you to figure out how to send the email using the form. There are several tutorials explaining how this is done in WordPress.
Those tutorials will probably direct you to create a function that handles the sending of the email from the form, this is where the actual email address is obtained, beyond the reach of the user. To pass the value to the email form, it is included in the URL, such as:
email-form/?recipient=2
Then, in the form, you have a hidden field that receives that value:<input type="hidden" name="recipient" value="<?php echo $_GET['recipient'] ?>" />
The function that receives the email form submission looks for the “recipient” value and uses that to get the email address.
- The topic ‘email field to point to an email form’ is closed to new replies.