I am trying to integrate a ‘subscribe to newsletter’ feature on my wordpress blog

Users just need to enter their name and email address, hit subscribe. This is then supposed to send me an email and I add them to the mailing list.

I activated the WP SMTP Mail plugin and entered in the relevant email server information to enable WordPress to send emails, asked WordPress to send me a test message and this worked.

Now I want to receive an email whenever someone fills in their name & email and hit subscribe.

If I do this at the moment it just does nothing, it re-loads the page without sending an email.

I have attached my code below, can anyone help me out here?

Code for the signup form sidebar.php - This is not a plugin.

 <form action="newsletter_signup.php" method="post">
     <p><input class="full" type="text" name="name" placeholder="Your name*"></p>
     <p><input class="full" type="email" name="Email" placeholder="Your email address*"></p>
     <p><input type="submit" class="sub-btn" value="Subscribe"></p>

The code in the newsletter_signup.php file:

    $name = $_POST['name'];
    $Email = $_POST['Email'];
    $to = '[email protected]';      
    wp_mail($to, $name, 'From: ' . $Email);
    echo 'Your request Has Been Sent, Thank You. ';
  • 3
    instead of calling a file newsletter_signup.php, it's better to use the wordpress hook. do you print the form with a widget or with a shortcode ?
    – mmm
    Aug 6, 2015 at 15:32
  • what wordpress hook should i use? Sorry i'm quite a newbie to this process. It's not a widget no, shortcode.
    – Chris
    Aug 6, 2015 at 15:36
  • in the shortcode make a form who submit at the same page and test the content of $_POST in the hook wp_loaded --- developer.wordpress.org/reference/hooks/wp_loaded
    – mmm
    Aug 6, 2015 at 15:43
  • my form is the HTML code above, sorry, that is inside the sidebar.php part of my blog. I'm not quite sure what you mean?
    – Chris
    Aug 6, 2015 at 15:53

2 Answers 2


This solution is based on a couple of WPMU Dev plugins: PopUp Pro and Mailchimp Integration. If you are WPMU Dev member then download and install them either from their Project pages or via the WPMU Dev dashboard in your WordPress admin interface. Most of the effort is in creating a custom style for the popup. The Mailchimp Integration plugin simply provides a shortcode that can be added to the body of the popup via a TinyMCE button but we’ll cover all this later. The first step, after installing the plugins is to configure the Mailchimp Integrationplugin.


The following is a simplistic approach to your question. It does not cover every possibility, but it does get things started.

This is a conditional statement that would go where you are putting the form. You didn't indicate exactly how you were getting the form in there, so this assumes you can run PHP from that location. If it's a text widget, then you may need to do something slightly different, but the core concept is the same.

So you have two states of display - one if the form is submitted and the other if it is not. If it's not, then obviously, you're displaying the form. But if the form has been submitted, then you're processing that data, sending the message, and displaying a success message.


// Check if the form was submitted.
$formsubmit = ( isset( $_POST['formsubmit'] ) ) ? true : false;

if ( $formsubmit ) {

    // If the form was submitted, process the email and
    // display success message.

    // Get the form values (don't forget to sanitize).
    $name  = sanitize_text_field( $_POST['name'] );
    $email = sanitize_email( $_POST['email'] );

    // Who is the message being delivered to?
    $to = '[email protected]';

    // Message subject and body:
    $subject  = "Web form signup";
    $message  = "The following signup was submitted: \r\n";
    $message .= "Name: " . $name . "\r\n";
    $message .= "Email: " . $email;

    // Send the message.
    wp_mail( $to, $subject, $message );

    // Display the success message.
    echo '<p>Your request Has Been Sent, Thank You.</p>';

} else { 

    // If the form was not submitted, display the form. ?>

     <form action="newsletter_signup.php" method="post">
         <p><input class="full" type="text" name="name" placeholder="Your name*"></p>
         <p><input class="full" type="email" name="email" placeholder="Your email address*"></p>
         <p><input type="submit" class="sub-btn" value="Subscribe"></p>
         <input type="hidden" name="formsubmit" value="1" />
<?php } ?>

This is not an "end-all be-all" solution. There are offshoots of this and different ways to approach it. My goal with this answer was to give a simple starting point that works, addresses the fact that most people forget to sanitize form input, and corrects the use of wp_mail() that was incorrect in the original question.

From here, there are additional possibilities. One of those would be to break up the handler and the display into separate processes so that you could display the HTML (i.e. via shortcode) based on whether the form was submitted, and you could handle all the PHP in a separate function. For that you'd need to put these elements in functions that were hooked to some filter and/or actions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.