From f1d89b018d9b56b52e7bc1a150bef0f4908ba097 Mon Sep 17 00:00:00 2001 From: Mehran Dehghanian Date: Fri, 15 Oct 2021 18:27:33 +0330 Subject: [PATCH] Add 'User Registration using WPForms' --- User-Registration-using-WPForms.md | 101 +++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 User-Registration-using-WPForms.md diff --git a/User-Registration-using-WPForms.md b/User-Registration-using-WPForms.md new file mode 100644 index 0000000..70a17f6 --- /dev/null +++ b/User-Registration-using-WPForms.md @@ -0,0 +1,101 @@ +Read a field from wpfroms: +``` +function wpf_dev_process_entry_save( $fields, $entry, $form_id, $form_data ) { + // Only run on my form with ID = 4117 + if ( absint( $form_data['id'] ) !== 4117 ) { + return $fields; + } + $_SESSION['age'] = $fields[3]['value']; // This is where we access the form data + $_SESSION['gender'] = $fields[2]['value']; +} +``` + +Sign up using snippets: +``` +$user_id = wp_insert_user( array( + 'user_login' => 'janedoe', + 'user_pass' => 'passwordgoeshere', + 'user_email' => 'jane.doe@example.com', + 'first_name' => 'Jane', + 'last_name' => 'Doe', + 'display_name' => 'Jane Doe', + 'role' => 'editor' +)); +``` +more information: +``` +$user_data = array( + 'ID' ( null | integer ), + 'user_pass' ( null | string ), + 'user_login' ( null | string ), + 'user_nicename' ( null | string ), + 'user_url' ( null | string ), + 'user_email' ( null | string ), + 'display_name'( null | string ), + 'nickname' ( null | string ), + 'first_name' ( null | string ), + 'last_name' ( null | string ), + 'description' ( null | string ), + 'user_registered' ( null | string ), + 'role' ( null | string ), + 'jabber' ( null | string ), + 'aim' ( null | string ), + 'yim' ( null | string ), + 'locale' ( null | string ) , + //... +); +``` + +Working example of signup and auto login combined together: +``` +/** + * This will fire at the very end of a (successful) form entry. + * + * @link https://wpforms.com/developers/wpforms_process_complete/ + * + * @param array $fields Sanitized entry field values/properties. + * @param array $entry Original $_POST global. + * @param array $form_data Form data and settings. + * @param int $entry_id Entry ID. Will return 0 if entry storage is disabled or using WPForms Lite. + */ + +function wpf_dev_process_complete( $fields, $entry, $form_data, $entry_id ) { + + // Optional, you can limit to specific forms. Below, we restrict output to + // form #5. + if ( absint( $form_data['id'] ) !== 130 ) { + return; + } + + $userFullName= $fields[7]['value']; + $codeMeli= $fields[48]['value']; + + //Create a user + // Update user data + $user_id = wp_insert_user( array( + 'display_name' => $userFullName, + 'user_login' => $codeMeli, + 'user_pass' => $codeMeli + )); +// $result = wp_create_user( $codeMeli, $codeMeli); +// if(is_wp_error($result)){ +// $error = $result->get_error_message(); + //handle error here +// }else{ +// $user = get_user_by('id', $result); + //handle successful creation here +// } + wp_signon( + array( + 'user_login' => $codeMeli, + 'user_password' => $codeMeli, + 'remember' => true, + ) + ); +} +add_action( 'wpforms_process_complete', 'wpf_dev_process_complete', 10, 4 ); +``` + +sources: +[Login/Signup snippets](https://usersinsights.com/create-wordpress-users-programmatically/#:~:text=The%20wp_create_user()%20function,get%20a%20WordPress%20error%20object.) +[Autologin after signup in wpfroms](https://wpforms.com/developers/automatically-log-in-users-after-registration/) \ No newline at end of file