How to connect registration form to database in WordPress - 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 ); ?>

Related

WordPress Contact form 7 entry to database without plugin

I tried with sort codes to enable database access form contact form 7 but I got failed.
is there any way to handle contact form 7 data's to database without any plugin or add on. I need to finish this through editing the plugin from backend. Can you please help me ??
<?php
function contactform7_before_send_mail( $form_to_DB ) {
//set your db details
global $wpdb;
$table = $wpdb->prefix.'candidate';
$form_to_DB = WPCF7_Submission::get_instance();
if ( $form_to_DB )
{
$formData = $form_to_DB->get_posted_data();
$firstname = $formData['first_name'];
$lastname = $formData['last_name'];
$phoneno = $formData['phone_no'];
$data = array('first_name' => $firstname, 'last_name' => $lastname, 'phone_no' => $phoneno);
$wpdb->insert( $table, $data, array( '%s','%s','%s' ) );
}
}
add_action( 'wpcf7_before_send_mail', 'contactform7_before_send_mail' );
?>

Autologin user by email only using wordpress

I am trying to login the user using the email only (The password is checked with an API from another system). The thing is all my code is running except for the part of autologin, below is my function:
I already tried different functionalities available online but what is happening with the below code is:
1- it will let the user login, but after refresh or going to any page which requires the user to be logged it, wordpress logs the user out.
function auth_new_user($username){
if(!username_exists($username)) {
// Generate the password and create the user
$password = wp_generate_password( 12, false );
$user_id = wp_create_user( $username, $password, $username );
// Set the nickname
wp_update_user(
array(
'ID' => $user_id,
'nickname' => $username
)
);
// Set the role
$user = new WP_User( $user_id );
$user->set_role( 'Subscriber' );
auto_login_usr($user_id,$username);
} // end if
else{
$the_user = get_user_by('email', $username);
$user_id = $the_user->ID;
echo $user_id;
auto_login_usr($user_id,$username);
}
}
function auto_login_usr($user_id, $username) {
$user = get_user_by( 'id', $user_id );
echo $user->user_login;
echo $user->password;
if( $user ) {
wp_set_current_user( $user_id, $user->user_login );
wp_set_auth_cookie( $user_id ,false, is_ssl());
do_action( 'wp_login', $user->user_login );
}
}
After logging in, WordPress automatically logs the user out ( keeps the user logged in for a second ). Note i debugged the code more than once and everything is appearing correctly ( User Id, User_login, and username).
Appreciate your help.
It is fine i fixed it myself, the headers was already sent. i should call the functions after_theme_setup:
either with a hook
Or Before calling get_header function

Create WP user from custom post type with action hook

I have a custom post type that I would like to use to create a WP user with an action hook. Once a user has completed the form the hook should enter the basic information to begin registering a wp user i.e. username, password email.
This is the function I have so far but I don't know why it is not working.
function agency_add_tenant( $post_id) {
$username = get_post_meta ( $post_id, 'username', true );
$password = get_post_meta ( $post_id, 'password', true );
$email_address = get_post_meta ( $post_id, 'email', true );
$user_id = function wp_create_user( $username, $password, $email_address );
// Email the user
wp_mail( $email_address, 'Welcome!', 'Your Password: ' . $password );
}

How to Auto Login After Registration in WordPress with core php

I've been trying for days now to take users who have just registered to my WordPress site and automatically log them in and then redirect them to a URL of my choice.
By default, WordPress sends you a username and a password, then you must log in manually. This is a total pain. How can i overcome this.
I have my own registration page(core php page) which successfully adds users into DB. But the point is, i should avoid users to login again.
Once registration is done, it should automatically redirects to home page or profile page.
I am a newbie to wordpress functionalities. It would be grateful if someone(who have knowledge on core functionality of wordpress) at least suggests a way/solution.
Looking forward.
Thanks
// Add on function.php for Auto login after register and redirect to a home page. varified this code
function auto_login_new_user( $user_id ) {
wp_set_current_user($user_id);
wp_set_auth_cookie($user_id);
$user = get_user_by( 'id', $user_id );
do_action( 'wp_login', $user->user_login );//`[Codex Ref.][1]
wp_redirect( home_url() ); // You can change home_url() to the specific URL,such as "wp_redirect( 'http://www.wpcoke.com' )";
exit;
}
add_action( 'user_register', 'auto_login_new_user' );
Following is based on how WooCommerce creates a new user and logs him in:
$user_pass = esc_attr( $_POST['account_password'] );
$new_user_data = array(
'user_login' => $_POST['account_username'],
'user_pass' => $user_pass,
'user_email' => $_POST['account_email'],
'role' => 'subscriber'
);
$user_id = wp_insert_user( $new_user_data );
// Set the global user object
$current_user = get_user_by( 'id', $user_id );
// set the WP login cookie
$secure_cookie = is_ssl() ? true : false;
wp_set_auth_cookie( $user_id, true, $secure_cookie );
to redirect use wp_safe_redirect, e.g.
wp_safe_redirect( home_url( '/' ) );
exit;
Instead of touching core files... You can use this
$secure_cookie = is_ssl();
$secure_cookie = apply_filters('secure_signon_cookie', $secure_cookie, array());
global $auth_secure_cookie;
$auth_secure_cookie = $secure_cookie;
wp_set_auth_cookie($user_id, true, $secure_cookie);
$user_info = get_userdata($user_id);
do_action('wp_login', $user_info->user_login, $user_info);
The function wp_create_user returns the just created user_id which you can use to create a cookie and log the user in. If you wish, you can redirect the logged in user to the profile or home page.
Thanks for your support guys..i did on my own with the following code..thanks for your time and support :)
<i>$getdetails= mysql_fetch_array(mysql_query("SELECT * FROM `wp_users` WHERE `ID`='$user_id'"));
$username=$getdetails['user_login'];
$creds = array();
$creds['user_login'] = $username;
$creds['user_password'] = $password;
$creds['remember'] = true;
$user = wp_signon( $creds, false );
if ( is_wp_error($user) ){
echo $user->get_error_message();
}else{
wp_redirect( home_url() );
}

wp_generate_password returns password but can't login using it

Im creating a simple login/registration page incorporating user moderation and bootstrapping wp for the heavy lifting.
I can get the new user added to the db and a password gets created and hashed and all, but when i go to login w/ that account the password won't work. i'm stumped...
However, when i go into the admin under my admin account and change the password for the user it allows me to login just fine...
Anyone see anything i'm missing?
require_once( ABSPATH . WPINC . '/registration.php' );
$user_pass = wp_generate_password();
$userdata = array(
'user_pass' => $user_pass,
'user_login' => esc_attr( $_POST['user_email'] ),
'user_email' => esc_attr( $_POST['user_email'] ),
);
if( !$userdata['user_login'] )
$error .= __('An Email Address is required for registration.', 'frontendprofile');
elseif ( username_exists( $userdata['user_login'] ) )
$error .= __('Sorry, that Email Address is already in use for another account.', 'frontendprofile');
elseif ( !is_email( $userdata['user_email'] ) )
$error .= __('You must enter a valid Email Address.', 'frontendprofile');
elseif ( email_exists( $userdata['user_email'] ) )
$error .= __('Sorry, that Email Address is already for another account.', 'frontendprofile');
else{
$new_user = wp_update_user( $userdata );
}
Now you need to use this
For Example:
$password = wp_generate_password( 8, false );
wp_set_password( $password, $user->ID );
Got it. Cant use wp_update_user for new addition i guess.
need to use:
wp_create_user( $userdata['user_login'], $userdata['user_pass'], $userdata['user_email'] );

Resources