0

I have a single email field form on a static, 1 page html website which upon submitting, I would like it to pass the entered email to a WordPress install to use as an email for a new account. The html website and the wordpress website are on different domains. Can WordPress handle this without a custom plugin?

I have the html of my form from the static website below:

<form action="signup.php" id="contact_form" method="post" onsubmit="return validateForm();">

<ul>

<li class="email">
<p class="white">Just enter your email to start. Easy, with spam-free goodness.</p>
<input type="text" name="email" id="email" value="Enter your e-mail" class="requiredField email" onblur="if(this.value == '') { this.value = 'Enter your e-mail'; }" onfocus="if(this.value == 'Enter your e-mail') { this.value = ''; }" />
<p class="whitesmall">Add additional details later, no credit cards required!</p>
</li>

<li class="button"><input id="submitted" class="input-submit" type="submit" value="Sign Up" name="submit" />
</li>

</ul>

</form>

Upon Submission, I have a javascript function which validates the email entered:

<script type="text/javascript">
function validateForm()
{
var x=document.forms["contact_form"]["email"].value;
var atpos=x.indexOf("@");
var dotpos=x.lastIndexOf(".");
if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
  {
  alert("Please supply a valid e-mail address");
  return false;
  }
}
</script>

The form action sets some variables and shoots me an email of the submitted email address:

<?php
$email_to = "[email protected]";
$emailSubject = 'User Account Submission from xxxxxxxxxxx';
$email = $_POST["email"];
$text = "Email: $email<br>";
header("Location:http://xxxxxxxxxxx.com/signup.html");
mail($email_to, $emailSubject, $text);
?>

How would I instead: 1) send this to WP so that WP can use the email to create an account 2) have wp setup a temp password 3) and then email the new account+pass to the user?

I'm still a beginner when it comes to customizing WordPress and am not sure the best way to go about sending WP the form?

Any help or insight on the proper places to look would be appreciated!

1 Answer 1

0

Sure WordPress can. But ... do you really want to do this? It could be a bit risky. OK, you want to do it, let's start with it.

At first you need a php file where you send the data to. Let's assume your submission form is on www.domain-send.tld and your WP install is on www.domain-receive.tld.

You have to create a php file on www.domain-receive.tld, name it e.g. remote_user_registration.php. You send the data from www.domain-send.tld with <form action="http://www.domain-receive.tld/path/to/remote_user_registration.php' method="post">. Put the php file in a subdirectory and protect it with a .htaccess from illegal requests

order deny allow
deny from all
allow from www.domain-send.tld

There are a lot of tutorials how to protect a directory and/or a singel file with a .htaccess. Please read some, this is not the topic of this question.

Now we create the php file. We have to include some files from WordPress so all needed functions are available, fetch the post data comes from www.domain-send.tld, do some checks and create an array with the user data. The last step is to insert the new user. Easy, isn't it? Here is the script:

<?php
// define some vars
if ( defined( 'ABSPATH' ) )
    $abspath = ABSPATH;
else
    $abspath = '/your/path/to/wordpress';

/*
 * define the role of the new user here
 * @see http://codex.wordpress.org/Roles_and_Capabilities
 */
$role = 'subscriber';

/*
 * fetch post data
 */
$user_email = ( isset( $_POST['email'] ) && ! empty( $_POST['email'] ) ) ?
    filter_input( INPUT_POST, 'email', FILTER_SANITIZE_EMAIL ) : '';

// no email, no registration!
if ( empty( $user_email ) ) {
// TODO: More error handling like an email to yourself or something else
    exit();
}

// needed to prevent wordpress to load the complete environment. we need only a basic wordpress
define( 'SHORTINIT', TRUE );

// include the needed files which are excluded by SHORTINIT
require_once $abspath . '/wp-load.php';
require_once $abspath . '/wp-includes/user.php';
require_once $abspath . '/wp-includes/pluggable.php';
require_once $abspath . '/wp-includes/formatting.php';
require_once $abspath . '/wp-includes/capabilities.php';
require_once $abspath . '/wp-includes/kses.php';
require_once $abspath . '/wp-includes/meta.php';
require_once $abspath . '/wp-includes/l10n.php';

// create a random password
$random_password = wp_generate_password( $length=12, $include_standard_special_chars=false );

/*
 * setup the registration data
 * here we use the user email as login name!
 * the minimum of data is user_pass (password) and user_login (login name)
 * 
 * @see http://codex.wordpress.org/Function_Reference/wp_insert_user
 */
$data = array(
        'user_pass'     => $random_password,
        'user_login'    => $user_email,
        'role'          => $role // optional but usefull if you create a special role for remote registered users
);

$new_user = wp_insert_user( $data );

Now you want to send the user a email with login name and password. And maybe you want to inform yourself if a user was registered remotely. Extend the code above a bit:

if ( ! is_wp_error( $new_user ) ) {

    $subject = "Remote registration to www.domain-receive.tld"; 
    $message = "Hi there! You have successfull registered to my blog. Your login name is {$user_email} and your password is {$random_password}\nPlease change your password immediately!";
    $headers = 'From: My Name <[email protected]>' . "\r\n";

    // @see http://codex.wordpress.org/Function_Reference/wp_mail
    $success = wp_mail( $user_email, $subject, $message, $headers, $attachments );

    // maybe you want to be informed if the registration was successfull
    if ( true == $success ) {
        wp_mail( '[email protected]', 'Remote registration', "User {$user_email} was registered on " . date ('d.m. Y H:i:s', time() ) );
    }
}

From now on everybody who see our submission form can register to your blog as subscriber (or whatever role you will use for new users). I think this is a bit risky.

4
  • thank you for the well written answer! I'm going to implement it and see how it works. I realize this is a bit risky now that you mention it as it does leave it open for quite the amount of Spam.. however I was looking for a way to simplify the WP account creation process (even more so :P) from outside of WordPress... Is there a better way of securing this style of signup while still getting the result I'm looking for? Apr 9, 2013 at 12:37
  • Use a double option in system. First create a hash from the email, create a link with this hash to your script and send this to the new user instead immediately registering. The user have to visit the link, if the hash (key) is present, registering the user. Spammer normally does not answer on emails, so they do not visit the link and do not automatically registering.
    – Ralf912
    Apr 11, 2013 at 16:13
  • So I tried to implement the answer above but I can't quite get it to run. I keep getting a 500 Internal Server Error. I updated the form action to point to the new file. I created the php file and placed it inside a /reg/ subfolder inside WP root folder so it's website.com/wpinstallfolder/reg/form_file.php What should I send the path to wordpress too? Am I missing something? Thanks! Apr 11, 2013 at 21:40
  • A 500 Internal Server Error is normally not a path problem, it's normally a misconfiguration of the server. It seems you have a problem with your .htaccess, look inside the server logs to find out what the exact problem is. But you can also try to include your wp-confifg.php with require_once '../wp-config.php'; (in the first line of your script) to setup ABSPATH correctly.
    – Ralf912
    Apr 12, 2013 at 11:41

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.