[英]How can I automatically create a Wordpress user when submitting a Gravity Form?
I am totally new to PHP. I am trying to automatically create a WordPress user when a form is submitted with a custom plugin using the following code:我是 PHP 的新手。我试图在使用以下代码使用自定义插件提交表单时自动创建 WordPress 用户:
add_action( 'gform_post_process', 'wp_create_user', 10, 3 );
function wp_create_user( $username, $random_password, $email ) {
$user_login = wp_slash( $entry[1]);
$user_email = wp_slash( $entry[2]);
$user_pass = wp_generate_password( $length = 12, $include_standard_special_chars = false );
$role = 'Cp Client';
$userdata = compact( 'user_login', 'user_email', 'user_pass' );
return wp_insert_user( $userdata );
}}
I have also tried with gform_after_submission and changing the function's name, but then my website breaks.我也尝试过使用 gform_after_submission 并更改函数的名称,但随后我的网站中断了。
What Am I doing wrong?我究竟做错了什么? Is this even possible?
这可能吗? Could someone offer me a code example, please?
有人可以给我一个代码示例吗?
Thanks in advance,提前致谢,
Paco帕科
You can try something like the following.您可以尝试以下操作。 (You may want to add a first, last and display name as well.) There is some more info here: https://usersinsights.com/create-wordpress-users-programmatically/
(您可能还想添加名字、姓氏和显示名称。)这里有更多信息: https://usersinsights.com/create-wordpress-users-programmatically/
add_action( 'gform_after_submission_185', 'register_a_user', 10, 2 );//replace 185 with the your form id
function register_a_user($entry, $form ){
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$user_id = wp_insert_user( array(
'user_login' => rgar( $entry, '1' ),
'user_pass' => substr(str_shuffle($chars),0,12),
'user_email' => rgar( $entry, '3' ),
'role' => 'cp-client'//check the slug name of the role
));
return;
}
With the help of Rochelle, I have come with a code that finally works.在 Rochelle 的帮助下,我得到了一个最终可以运行的代码。 This is it:
就是这个:
add_action( 'gform_after_submission', 'create', 10, 2 );
function create( $entry, $form ) {
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$user_login = wp_slash( $entry[3] );
$user_email = wp_slash( $entry[2] );
$user_pass = substr(str_shuffle($chars),0,12);
$userdata = compact( 'user_login', 'user_email', 'user_pass' );
return wp_insert_user( $userdata );
}
Thanks to everyone who left a comment on this post!感谢所有对此帖子发表评论的人!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.