简体   繁体   English

如何在提交Gravity Form时自动创建Wordpress用户?

[英]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.

相关问题 Gravity Forms - 如果用户已经提交了一次表单,我如何显示默认消息而不是重力表单? - Gravity Forms - How can I show a default message instead of a Gravity Form if the user has already submitted the form once? 如何在Wordpress中删除“重力表单用户注册”中的密码确认字段? - How can I remove the password confirmation field in Gravity Forms User Registration in Wordpress? 如何在 WordPress 中创建自定义表单? - How can I create a custom form in WordPress? 提交表单时如何防止模态隐藏? - How can I prevent modal to hide when submitting form? 我该如何访问在Gravity Forms中完成填写注册表的用户的ID? - How can I access the ID of the user that just completed registration form in Gravity Forms? 提交表格后如何自动创建客户表 - How to create a client table automatically after submitting form 我可以定义自己的表单标记并映射到使用Gravity Form(wordpress插件)生成的表单吗? - Can I Define my own form markup and map with to the form generated using Gravity Form (wordpress plugin)? 提交表单时如何自动将FactoryID和CategoryID插入表格中-Codeigniter - How to automatically insert factoryid and categoryid into a table when submitting form - codeigniter 虽然用户已登录wordpress,但要求用户登录才能使用重力表格 - Require user to be logged in not working for gravity form though user is logged in wordpress 如何在Wordpress Gravity Forms中使用PHP创建条目 - How to create an entry with PHP in Wordpress Gravity Forms
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM