The requirements for using this snippet are only the use of the plugin:
And the use of a Child Theme.
It is not advisable to place custom code inside functions.php the main theme, because the code disappears whenever the theme is updated.
function criar_user_from_cf7($cfdata) {
if (!isset($cfdata->posted_data) && class_exists('WPCF7_Submission')) {
$submission = WPCF7_Submission::get_instance();
if ($submission) {
$formdata = $submission->get_posted_data();
}
} elseif (isset($cfdata->posted_data)) {
$formdata = $cfdata->posted_data;
} else {
return $cfdata;
}
We start by opening a function, putting this small excerpt of code for now.
What it does is link between the results submitted by the CF7 form.
if ( $cfdata->title() == 'Registo para revendedores') {
$password = wp_generate_password( 12, false );
$name = $formdata['nome'];
$email = $formdata['email'];
$empresa = $formdata['empresa'];
$morada = $formdata['Morada'];
$cod_postal = $formdata['cod_postal'];
$cidade = $formdata['cidade'];
$pais = $formdata['pais'];
$telefone = $formdata['telefone'];
$telemovel = $formdata['telemovel'];
$nif = $formdata['nif'];
In this part we will select the form by it’s name which should already be created before in CF7, in this case the name of the form is “Registo para revendedores”.
We will then select the fields by placing them in variables, the fields are dependent on the form created in this case we have the following fields:
- password -> In the password field is used the function “wp_generate_password” which requires the following parameters password length (12) and if the password takes special characters (false) this password will be a temporary password since the reseller at the end will create your password.
- name > equivalent to the “nome” field in CF7
- email-> equivalent to the “email” field in CF7
- empresa-> equivalent to the “empresa” field in the CF7
- morada-> equivalent to the “Morada” field in cf7
- cod_postal-> equivalent to the “cod_postal” field in the CF7
- cidade-> equivalent to the “cidade” field in the CF7
- pais-> equivalent to the “pais” field in the CF7
- telefone-> equivalent to the “telefone” field in the CF7
- telemovel-> equivalent to the “telemovel” field in the CF7
- nif-> equivalent to the “nif” field in cf7
// Construct a username from the user's name
$username = strtolower(str_replace(' ', '', $name));
$name_parts = explode(' ',$name);
if ( !email_exists( $email ) ) {
// Find an unused username
$username_tocheck = $username;
$i = 1;
while ( username_exists( $username_tocheck ) ) {
$username_tocheck = $username . $i++;
}
$username = $username_tocheck;
// Create the user
$userdata = array(
'user_login' => $username,
'user_pass' => $password,
'user_email' => $email,
'nickname' => reset($name_parts),
'display_name' => $name,
'first_name' => reset($name_parts),
'last_name' => end($name_parts),
'role' => 'validar'
);
$user_id = wp_insert_user( $userdata );
//Fazer update meta data após criação da conta
update_user_meta($user_id, "billing_country", $pais );
update_user_meta($user_id, "shipping_country", $pais );
update_user_meta($user_id, "billing_postcode", $cod_postal );
update_user_meta($user_id, "shipping_postcode", $cod_postal );
update_user_meta($user_id, "billing_nif", $nif );
update_user_meta($user_id, "billing_phone", $telemovel );
update_user_meta($user_id, "billing_company", $telemovel );
A username will then be created using the name placed, first checking if there is already any previously created, after confirming that it does not exist, then the creation of the account.
After the creation of the account are then inserted the user_meta used in woocommerce, you can also referenciate here custom user_meta as it is in the case of “nif”
if ( !is_wp_error($user_id) ) {
// Email login details to user
$blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
$message = "Bem Vindo! Os detalhes do seu login atuais são:" . "\r\n";
$message .= sprintf(__('Nome de Utilizador: %s'), $username) . "\r\n";
$message .= "A sua conta está em processo de validação." . "\r\n";
$message .= "Poderá fazer login no link abaixo após validação." . "\r\n";
$message .= wp_login_url() . "\r\n";
wp_mail($email, sprintf(__('[%s] A sua conta encontra-se em validação'), $blogname), $message);
}
}
}
return $cfdata;
}
add_action('wpcf7_before_send_mail', 'criar_user_from_cf7', 1);
In the end if there was no error with the creation of the account, the following email will be sent to the client.
It is also created an account in backoffice with the role defined in the previous snippet code in this case it being “validar”
//SEND EMAIL WHEN THE ROLE IS UPDATED
function email_role_exchange( $user_id, $new_role ) {
$site_url = get_bloginfo('wpurl');
$user_info = get_userdata( $user_id );
$username = $user_info->user_login;
$user = new WP_User( (int) $user_id );
$adt_rp_key = get_password_reset_key( $user );
$rp_link = network_site_url("wp-login.php?action=rp&key=$adt_rp_key&login=$username" . rawurlencode($user_login), 'login');
if (user_can( $user_id, 'revendedor' ) ) {
// Email login details to user
$blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
$message = "Este e-mail confirma a sua validação " . $user_info->display_name . "\r\n" . "\r\n";
$message .= sprintf(__('Nome de Utilizador: %s'), $username) . "\r\n" . "\r\n";
$message .= "Poderá criar a sua password no seguinte link: " . $rp_link . "\r\n" . "\r\n";
$message .= "Poderá fazer login no link abaixo após validação." . "\r\n" . "\r\n";
$message .= wp_login_url() . "\r\n" . "\r\n";
wp_mail($user_info->user_email , sprintf(__('[%s] Confirmação de Validação de Conta'), $blogname), $message);
}
}
add_action( 'set_user_role', 'email_role_exchange', 10, 2);
Finally, after we close the previous function. we can create a distinct one whose purpose is to send email to the customer (reseller) when the role is updated from “validar” to “revendedor” by sending a password creation link.
This finishes the tutorial to create accounts through contact form 7, automatically only needing to see if the data entered by the client is correct and being able to send all the data for password creation at the touch of a button.
Any questions regarding the post, can leave a comment below or send email to a.fernandes@afcreations.pt

This Post Has 0 Comments