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