从外部电子邮件表单提交创建WP帐户?

时间:2013-04-04 作者:VirtuallyCreative

我在一个静态的、1页html网站上有一个单一的电子邮件字段表单,提交后,我希望它将输入的电子邮件传递给WordPress安装,用作新帐户的电子邮件。html网站和wordpress网站位于不同的域上。WordPress能在没有自定义插件的情况下处理这个问题吗?

我从以下静态网站获得了我表单的html:

<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>
提交后,我有一个javascript函数来验证输入的电子邮件:

<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>
表单操作设置一些变量,并向我发送一封提交电子邮件地址的电子邮件:

<?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);
?>
我应该如何取而代之:1)将此发送给WP,以便WP可以使用电子邮件创建帐户2)让WP设置临时密码3),然后通过电子邮件将新帐户+密码发送给用户?

在定制WordPress时,我还是个初学者,不确定发送WP表单的最佳方式是什么?

如果您能在合适的地方提供帮助或见解,我们将不胜感激!

1 个回复
最合适的回答,由SO网友:Ralf912 整理而成

当然WordPress可以。但是你真的想这样做吗?这可能有点冒险。好吧,你想这样做,让我们从它开始。

首先,您需要一个php文件,将数据发送到该文件。假设您的提交表单位于www.domain-send上。tld和您的WP安装在www.domain-receive上。tld。

您必须在www.domain-receive上创建一个php文件。tld,命名为,例如。remote_user_registration.php. 您可以从www.domain-send发送数据。tld带<form action="http://www.domain-receive.tld/path/to/remote_user_registration.php\' method="post">. 将php文件放在子目录中,并使用.htaccess 来自非法请求

order deny allow
deny from all
allow from www.domain-send.tld
有很多教程介绍如何使用.htaccess. 请读一些,这不是这个问题的主题。

现在我们创建php文件。我们必须包括WordPress的一些文件,以便所有需要的功能都可用,获取来自www.domain-send的帖子数据。tld,执行一些检查并使用用户数据创建一个数组。最后一步是插入新用户。很简单,不是吗?以下是脚本:

<?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 );
现在,您要向用户发送一封包含登录名和密码的电子邮件。如果用户是远程注册的,您可能想通知自己。将代码扩展一点以上:

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() ) );
    }
}
从现在起,所有看到我们提交表单的人都可以作为订阅者注册到您的博客上(或者您将为新用户使用的任何角色)。我认为这有点冒险。

结束

相关推荐

Send email only upon draft

我正在尝试仅在将帖子保存为草稿时发送电子邮件,这似乎与当前代码不兼容: add_action( \'save_post\', \'er_send_email_on_post_draft_save\' ); function er_send_email_on_post_draft_save( $post_id ) { //verify post is not a revision if ( $post_id->post_status