你想做的事情可以通过两个步骤来实现。首先,创建一个注册表,然后将响应发送到服务器并创建用户。
既然你有JS和HTML的技能,我就直接谈正事。下面是一份非常简单的注册表。您将暂时将此表单设置为隐藏。
登记表
<div id="ajax-register-modal">
<input type="text" id="ajax-username" placeholder="<?php _e( \'Username\', \'text-domain\' ); ?>"/>
<input type="password" id="ajax-password" placeholder="<?php _e( \'Password\', \'text-domain\' ); ?>"/>
<span id="register-modal-send"><?php _e( \'Register\', \'text-domain\' ); ?></span>
</div>
然后,当您单击该链接时,jQuery将使其可见。
jQuery(\'#YourLinkID\').click(function($){
$(\'#ajax-register-modal\').toggle();
});
所以当我们点击链接时,它会弹出。现在,我们在用户单击register按钮时发送数据。
让我们将一个操作绑定到按钮:
jQuery(\'#register-modal-send\').click(function($){
registerTheUser();
});
服务器端注册现在AJAX处理程序:
function registerTheUser(){
// Get the field values for username and password
var username = $(\'#ajax-username\').val();
var password = $(\'#ajax-password\').val();
// Send them to server via AJAX
jQuery.ajax({
type: \'GET\',
url: \'REST URL HERE\',
data: { ajaxUsername: username , ajaxPassword: password},
});
}
但是等等,数据会去哪里?最后一个阶段是在后端创建一个REST处理程序,以处理注册。我们只需创建一个REST路由并注册用户。
add_action( \'rest_api_init\', function () {
//Path to REST route for registration
register_rest_route( \'LarryG001/v2\', \'/ajax_registration/\', array(
\'methods\' => \'GET\', // I suggest using POST
\'callback\' => \'register_the_user\' // Callback PHP function
) );
});
好了,开始注册吧!
function register_the_user(){
// Get the username and password
$username = $_GET[\'ajaxUsername\'];
$password = $_GET[\'ajaxPassword\'];
// A basic creation of user based on username and password. Email is optional
$id = wp_create_user( $username, $password, $email );
// Return the ID of the new user
return $id;
}
还记得AJAX调用中的URL吗?这是我们应该在那里使用的,因为我们的请求指向以下URL:
http://example.com/wp-json/LarryG001/v2/ajax_registration/
完成。我们刚刚使用创建了一个新用户
wp_create_user 作用
这里我省略了很多内容,例如检查值是否已设置(通过使用if(isset($_GET[\'ajaxUsername\']) ).
返回数据后,可以将其用作AJAX响应。