How is wordpress user validation works? - wordpress

I have created a custom registration form in the front end using wp_insert_user() function:
$data = array();
$data['user_login'] = $_POST['username'];
$data['user_email'] = $_POST['email'];
$data['user_pass'] = wp_generate_password ( 12, false );
$data['role'] = 'pending';
$user = wp_insert_user( $data );
if ( is_wp_error($user) ){
$error = $user->get_error_message();
echo json_encode( array( 'loggedin' => false, 'info' => $error ) );
} else {
wp_new_user_notification( $user, $data['user_pass'] );
}
But I want the user to receive an activation email with activation link. How can I do this?

If you look at the WordPress codex for the wp_new_user_notification method (http://codex.wordpress.org/Function_Reference/wp_new_user_notification#Examples), there is an example of how to redefine this function. In that example, functionality is added to send an email to the created user.

Related

Update ACF field based on user's email domain

I need some help and I can't seem to figure it out since I'm not exactly sure how to do this.
I have a custom post type called Companies. In the admin user interface, I have created an ACF relationship field to the Companies post type called user_company. Right now I am manually selecting the Companies post based on the user's email address and I would like to do this programatically.
`
function update_user_company( $user_id, $args ) {
$user = get_user_by( 'ID', $user_id );
$email = $user->user_email;
$company = get_field('user_company');
list($user, $domain) = explode('#', $args['user_email'] );
if ($domain == 'gmail.com') {
$company[] = 'Google';
update_field('user_company', $company, $post_title);
};
if ($domain == 'amyling.com') {
$company[] = 'Pearlsin Arts';
update_field('user_company', $company, $post_title);
};
}
add_action( 'user_register', 'update_user_company', 10, 2 );
`
I think your function is correct and the only thing that seems wrong is that you try to update a custom field based on a post title and you should use the post ID or in your case the user ID instead.
So you should try this:
function update_user_company( $user_id, $args ) {
$user = get_user_by( 'ID', $user_id );
$email = $user->user_email;
$company = get_field('user_company');
list($user, $domain) = explode('#', $args['user_email'] );
if ($domain == 'gmail.com') {
$company[] = 'Google';
update_field('user_company', $company, $user_id);
};
if ($domain == 'amyling.com') {
$company[] = 'Pearlsin Arts';
update_field('user_company', $company, $user_id);
};
}
add_action( 'user_register', 'update_user_company', 10, 2 );
Hope this helps

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' );
?>

WordPress additonal check while login

When a user register in my site I have set the user_activation_key column value from wp_users table like that:
$code = sha1( $user_id . time() );
global $wpdb;
$wpdb->update(
$wpdb->prefix.'users', //table name
array( 'user_activation_key' => $code ),
array( 'ID' => $user_id ),
array( '%s' ),
array( '%d' )
);
It's because I want to make activation system by sending an email with clickable link:
$activation_link = add_query_arg(
array(
'key' => $code,
'user' => $user_id
), get_permalink( 44 )
);
$message = "<div style='padding : 20px; border : 1px solid #ddd; color : #000;'>Hello $surname, <br/><br/>Please confirm your email addresss . Click this link to confirm : <a href='$activation_link'>Confirm Now</a><br/><br/></div>";
$to = $email;
$subject = 'Confirm your registration process"';
$body = $message;
$headers = array('Content-Type: text/html; charset=UTF-8');
wp_mail( $to, $subject, $body, $headers );
Now, the column user_activation_key has the hash code and user_status column value is 0
Now the actual question:
When user go to www.site.com/wp-admin that means login page I want to show an error message if the user_status column value is 0.
I don't' have any idea which hook or how can I cehck this while user login?
Use the admin_notices hook to display messages in the backend, the code below is similar to what your after:
add_action('admin_notices', 'account_activation_check');
function account_activation_check() {
global $wpdb;
// setup vars //
$currentID = get_current_user_id();
$user = get_user_by( 'ID', $currentID );
$userStatus = $user->user_status;
// check if user status is 0 //
if($userStatus == 0) {
echo '<div class="error"><p>Your email has not been verified!</p></div>';
}
}
I have solved the issues by using the wp_authenticate_user hook. Here is the code:
add_filter( 'wp_authenticate_user', 'shibbir_authenticate_user', 10, 2 );
function shibbir_authenticate_user( $user ) {
if ( $user->data->user_status == 0 ) {
return new WP_Error( 'error', __( 'Your account is not activate, Please contact site admininstrator.' , 'shibbir' ) );
}
return $user;
}

Redirect user based on custom metadata, wordpress/woocommerce

I have added some custom metadata to wordpress per user and would like to redirect each user after login based on this custom metadata.
For instance i have a page called foo and another called bar. User one has a custom key (acCustomerName) in the metadata stating foo. And the other user has bar in his.
Depending on whats inside that field I would like to redirect the first user to http://example.tld/foo and the other to http://example.tld/bar.
I am able to retrieve this information perfectly fine and echo it out directly to test the function but are not able to retrieve that information inside the sencond function named forward_user and would like to know why.
Right now the user is redirected only to http://example.tld only. So it seems like the function does not get the $kundtyp string.
function kundtyp($Uuser) {
$key = 'acCustomerName';
$single = true;
return get_user_meta( $Uuser, $key, $single );
}
function forward_user( $redirect, $user ) {
$kundtyp = kundtyp($user);
$redirect_page_id = url_to_postid( $redirect );
$checkout_page_id = wc_get_page_id( 'checkout' );
if( $redirect_page_id == $checkout_page_id ) {
return $redirect;
}
return site_url( kundtyp( get_current_user_id() ) );
}
add_filter( 'woocommerce_login_redirect', 'forward_user', 1100, 2 );
Here is the updated code, you have to pass the ID of the user object to get_user_meta data function. You were passing the WP_User object to your function. I don't think you can use function get_current_user_id() because the global wp_user object has not been set yet. I checked it and it always returns 0 as the value. This would explain why they provide the $user variable in the filter.
function kundtyp($Uuser) {
$key = 'acCustomerName';
$single = true;
return get_user_meta( $Uuser->ID, $key, $single );
}
function forward_user( $redirect, $user ) {
$kundtyp = kundtyp($user);
$redirect_page_id = url_to_postid( $redirect );
$checkout_page_id = wc_get_page_id( 'checkout' );
if( $redirect_page_id == $checkout_page_id ) {
return $redirect;
}
return site_url( kundtyp( $user ) );
}
add_filter( 'woocommerce_login_redirect', 'forward_user', 1100, 2 );

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() );
}

Resources