Store contactform 7 data in to Users - wordpress - wordpress

I'm working on the WordPress website where I want to create the registration form with contact form 7 plugin, users details are stored in the database but unable login to with details which are stored by the contact form 7 plugin into the database?
Any idea how can user login with details which are stored by the contact form 7 plugin?
Contact Form 7 Form Example
<label>Email (required)
[text* your-email] </label>
<label>Phone(required)
[text* your-phone] </label>
<label>Password(required)
[password* pass] </label>
Error
ERROR: The password you entered for the email address test#yahoo.com
is incorrect. Lost your password?

You need to add the code in wpcf7_before_send_mail hook, please have a look at below completed working code.
add_action('wpcf7_before_send_mail', 'save_form' );
function save_form( $wpcf7 ) {
global $wpdb;
/*
Note: since version 3.9 Contact Form 7 has removed $wpcf7->posted_data
and now we use an API to get the posted data.
*/
$submission = WPCF7_Submission::get_instance();
if ( $submission ) {
$submited = array();
$submited['posted_data'] = $submission->get_posted_data();
}
$email=$submited['posted_data']['your-email'];
$pass=$submited['posted_data']['pass'];
$userdata = array(
'user_login' => $email,
'user_email' => $email,
'user_pass' => $pass // When creating an user, `user_pass` is expected.
);
$user_id = wp_insert_user( $userdata ) ;
}

Related

logged-in user’s information in contact for 7 plugin

I have 2 users in WordPress. One is admin and another is a contributor.
I am trying to send email using the contact form 7 plugin.
I logged in by contributor user. I need to send user (contributor) details like email. I use a code default:user_display_name and [email* your-email default:user_email] in message body field. Both of them are not useful.
If I use [_site_admin_email] email of admin is sent. I want to send the logged-in user email.
You should use wpcf7_before_send_mail action hook to add user's info in your email body. Below is an example on how you can do that.
add_action( 'wpcf7_before_send_mail', 'wpcf7_add_text_to_mail_body' );
function wpcf7_add_text_to_mail_body($contact_form){
$form_id = $contact_form->posted_data['_wpcf7']; // Get for ID
if ($form_id == 123): // 123 => Your Form ID.
$current_user = wp_get_current_user(); // Get Current User Object
$user_email = $current_user->user_email;
// get mail property
$mail = $contact_form->prop( 'mail' ); // returns array
// add content to email body
$mail['body'] .= 'User Email Address: ';
$mail['body'] .= $user_email;
// set mail property with changed value(s)
$contact_form->set_properties( array( 'mail' => $mail ) );
endif;
}

How to get contact email from submitted data in Contact Form 7?

I would like to subscribe the user to my other newsletter system using Contact Form 7. I tried to get the recipient email from the submitted form with the below code, but it returns sender (admin) email.
add_action('wpcf7_before_send_mail', function ($contact_form) {
$mailProp = $contact_form->get_properties('mail');
subscribe_to_another_newsletter($mailProp['mail']['recipient']);
});
How can I get the contact's data?
You want to grab your submission and then use the form field you used for email to do what you want with it.
add_action( 'wpcf7_before_send_mail', array($this, 'cf7_process_form'));
function my_cf7_process_form(){
// This calls the static get the cf7 form data
$submission = WPCF7_Submission::get_instance();
if ( $submission ) {
// $posted_data = array with all form fields
$posted_data = $submission->get_posted_data();
}
// if [your-email] is the form tag
$email = $posted_data['your-email'];
subscribe_to_another_newsletter($email);
}

STOP sending new user registration password as plain text in Wordpress through email

When a new user register on a WooCommerce site user got an email about confirmation message which contain the current registration info with username and password. The main issue is the password is pain text like
Username = xcorola
Password = 123456
How can I stop sending plain text password to user email. or is there any way to override the wp_new_user_notification() ?
$message = sprintf(__('Username: %s'), $user - > user_login)."\r\n";
$message. = sprintf(__('Password: %s'), $plaintext_pass)."\r\n";
I saw your question as I ran into to the exact same problem! I have spend over 10 hours digging into the Wordpress backend to find the responsible function, and lucky for anyone in the future, I found the following neat solution!
How to make Wordpress new user registration email contain a 'set password'-link, instead plain-text password:
In the first step we will create our own function to return a set-password url. You can simply add this codeblock in Themes → Theme editor → functions.php (right pane)
/**
* This function invokes the get_password_reset_key to invoke generation of a nonce in the backend db,
* and returns the key. It then constructs a http query to the lost-password endpoint
* M.G.Poirot 2021
*/
function get_password_reset_link( $user_login = null ) {
$base_url = wc_get_endpoint_url( 'lost-password', '', wc_get_page_permalink( 'myaccount' ) );
$userdata = get_user_by('login', $user_login);
$user_id = $userdata -> ID;
$key = get_password_reset_key( $userdata );
if ( ! is_wp_error( $key ) ) {
$base_url .= '?' . http_build_query( array ( 'key' => $key, 'id' => $user_id ) ) ;
}
// If retrieving the key fails, we still return the url that sends the user
// to enter their e-mail and start the regular password reset procedure.
return $base_url;
}
In this second step we will make the e-mail template editable that is currently sending plain text passwords. For me I had to follow the following buttons, but it might differ depending on your application:
Go to Woocommerce → Settings → E-mails → New account → Manage
Press Copy to Theme. This will make it possible to edit this
e-mail template
Now go to this template. You can find it at Themes → Theme editor → Theme files (right pane) → woocommerce → emails → customer_new_account.php
Here, replace:
 
<p>
<?php printf( esc_html__( 'Your password has been automatically generated: %s', 'woocommerce' ), '<strong>' . esc_html( $user_pass ) . '</strong>' ); ?>
</p>
With:
<p>
<a class="link" href="<?php echo ; ?>"><?php // phpcs:ignore ?>
<?php esc_html_e( 'Click here to set your password', 'woocommerce' ); ?>
</a>
</p>
Hope this works for everyone. People with more knowledge about PHP can probably add the clarity and robustness of the function!

How Can I add Email Verification Functions For WooCommerce

I would like to add Email verification procedure when user registers in WooCommerce. WordPress then emails a verification link to user's email. If link is clicked, it then activates the user's account. How would I do that?
I have used the code provided by Amit Kayshap and refined it to include extra checks and functions like automatically logging a user in after their account has been activated, resulting in a much smoother user experience.
Update: Unlike the original code, this one will not require any existing user to confirm their email address as well.
Like the code I based it upon, it is designed to run on a WordPress installation running WooCommerce. It also works if you have disabled the standard WordPress registration page.
You'll need an empty page with the URL yoursite.com/verify/ that builds on a template that contains <?php wc_print_notices(); ?> within its content container. It'll replace the /sign-in/ destination from the original code and will handle almost all messages created by this code.
Next, add this code to your theme's functions.php:
function wc_registration_redirect( $redirect_to ) { // prevents the user from logging in automatically after registering their account
wp_logout();
wp_redirect( '/verify/?n=1'); // redirects to a confirmation message
exit;
}
function wp_authenticate_user( $userdata ) { // when the user logs in, checks whether their email is verified
$has_activation_status = get_user_meta($userdata->ID, 'is_activated', false);
if ($has_activation_status) { // checks if this is an older account without activation status; skips the rest of the function if it is
$isActivated = get_user_meta($userdata->ID, 'is_activated', true);
if ( !$isActivated ) {
my_user_register( $userdata->ID ); // resends the activation mail if the account is not activated
$userdata = new WP_Error(
'my_theme_confirmation_error',
__( '<strong>Error:</strong> Your account has to be activated before you can login. Please click the link in the activation email that has been sent to you.<br /> If you do not receive the activation email within a few minutes, check your spam folder or click here to resend it.' )
);
}
}
return $userdata;
}
function my_user_register($user_id) { // when a user registers, sends them an email to verify their account
$user_info = get_userdata($user_id); // gets user data
$code = md5(time()); // creates md5 code to verify later
$string = array('id'=>$user_id, 'code'=>$code); // makes it into a code to send it to user via email
update_user_meta($user_id, 'is_activated', 0); // creates activation code and activation status in the database
update_user_meta($user_id, 'activationcode', $code);
$url = get_site_url(). '/verify/?p=' .base64_encode( serialize($string)); // creates the activation url
$html = ( 'Please click here to verify your email address and complete the registration process.' ); // This is the html template for your email message body
wc_mail($user_info->user_email, __( 'Activate your Account' ), $html); // sends the email to the user
}
function my_init(){ // handles all this verification stuff
if(isset($_GET['p'])){ // If accessed via an authentification link
$data = unserialize(base64_decode($_GET['p']));
$code = get_user_meta($data['id'], 'activationcode', true);
$isActivated = get_user_meta($data['id'], 'is_activated', true); // checks if the account has already been activated. We're doing this to prevent someone from logging in with an outdated confirmation link
if( $isActivated ) { // generates an error message if the account was already active
wc_add_notice( __( 'This account has already been activated. Please log in with your username and password.' ), 'error' );
}
else {
if($code == $data['code']){ // checks whether the decoded code given is the same as the one in the data base
update_user_meta($data['id'], 'is_activated', 1); // updates the database upon successful activation
$user_id = $data['id']; // logs the user in
$user = get_user_by( 'id', $user_id );
if( $user ) {
wp_set_current_user( $user_id, $user->user_login );
wp_set_auth_cookie( $user_id );
do_action( 'wp_login', $user->user_login, $user );
}
wc_add_notice( __( '<strong>Success:</strong> Your account has been activated! You have been logged in and can now use the site to its full extent.' ), 'notice' );
} else {
wc_add_notice( __( '<strong>Error:</strong> Account activation failed. Please try again in a few minutes or resend the activation email.<br />Please note that any activation links previously sent lose their validity as soon as a new activation email gets sent.<br />If the verification fails repeatedly, please contact our administrator.' ), 'error' );
}
}
}
if(isset($_GET['u'])){ // If resending confirmation mail
my_user_register($_GET['u']);
wc_add_notice( __( 'Your activation email has been resent. Please check your email and your spam folder.' ), 'notice' );
}
if(isset($_GET['n'])){ // If account has been freshly created
wc_add_notice( __( 'Thank you for creating your account. You will need to confirm your email address in order to activate your account. An email containing the activation link has been sent to your email address. If the email does not arrive within a few minutes, check your spam folder.' ), 'notice' );
}
}
// the hooks to make it all work
add_action( 'init', 'my_init' );
add_filter('woocommerce_registration_redirect', 'wc_registration_redirect');
add_filter('wp_authenticate_user', 'wp_authenticate_user',10,2);
add_action('user_register', 'my_user_register',10,2);
If you are running a multilingual site, you can make the code translation-ready very easily. Just change the text strings like this: __( 'Text you want to translate', 'your-theme' ) This allows translation plugins like WPML to add the string to a translation table in the your-theme text domain.
Note that any string containing a variable like .$url. will generate a new string every time a different user activates its function. To circumvent this (and prevent string spamming into your database), we can translate them directly in the code:
if(ICL_LANGUAGE_CODE=='de'){
wc_add_notice( __( 'German error message' ), 'error' );
} else {
wc_add_notice( __( 'English error message' ), 'error' );
}
In this example, the german message will be output if the user's language code is detected as de (Also works if it is a variation like de_DE_formal), else it will output the english message.
Edit: I updated the code to not require an existing user to retroactively confirm their email address.

How to connect registration form to database in WordPress

How to connect registration form to database in WordPress
Connect registration page in WordPress to MySQL databases?
Create Your custom form and use wp_insert_user function for user registration.
<?php
$website = "http://example.com";
$userdata = array(
'user_login' => 'login_name',
'user_url' => $website,
'user_pass' => $password,
);
$user_id = wp_insert_user( $userdata ) ;
//On success
if ( ! is_wp_error( $user_id ) ) {
echo "User created : ". $user_id;
}
?>
To add a user use ether wp_insert_user or wp_create_user. wp_create_user ONLY creates a user, and does not allow for managing any extra user fields. wp_insert_user allows for filling in all user fields.
<?php wp_insert_user( $userdata ); ?>

Resources