After user registered in wordpress the user should be inactive - wordpress

In registration, after user registered, they should confirm the email validation link, until they validate the email their role should be inactive and they should not able to login in to the website.
Am adding the user using wp_insert_user(), in this function there is a field name call 'role' but I don't know what should be given there. I tried "inactive" but not working. Kindly suggest me.

Assuming your new users are assigned the 'subscriber' role on registration, you should hook to the 'user activated' hook and do something like:
wp_update_user( array ( 'ID' => $user_id, 'role' => 'author' ) ) ;
In your application you should ensure the 'subscriber' is locked-down or inactive (i.e. can't do anything much) and your 'author' role has the full set of capabilities that you need your active users to have.

Related

Secure way to let user create their own password with ACF registration form in Wordpress?

I'm looking to allow a user to create their own password on a custom front-end registration form.
I've created a front-end user registration form with ACF.
Currently the password is generated on the backend like so:
$password = wp_generate_password( 10, true, false );
$userdata = array(
'user_login' => $email,
'user_email' => $email,
'display_name' => $display_name,
'user_pass' => $password,
'role' => 'puppydog',
);
$created_user_id = wp_insert_user( $userdata );
It's a simple thing to do grab a password field from the front-end, but I'm wondering if it's safe to just take that password field, escape & hash it, and store it in the database? Or is there a more secure way to do it?
I noticed the answer here seems to just take the password from the front-end and create a user with it "raw," but I want to make sure this is actually reasonably safe to do?

Add alternate customer role to WordPress / WooCommerce

I am trying to add another customer role to WordPress and WooCommerce. I will use this new customer role to assign alternate prices when the user is logged in. My code works but I cannot find what permissions a customer has in WordPress / WooCommerce by default. I want this new role to have identical permissions to the default customer account. The code below is located in my child functions.php file.
/* Custom user roles */
add_role('distributor', __(
'Distributor'),
array(
'read' => true, // Allows a user to read
'create_posts' => true, // Allows user to create new posts
'edit_posts' => true, // Allows user to edit their own posts
'edit_others_posts' => true, // Allows user to edit others posts too
'publish_posts' => true, // Allows the user to publish posts
'manage_categories' => true, // Allows user to manage post categories
)
);
You can use the capabilies of another role and use this as the "capabilities array" when you create the new user role with the add_role() function of wordpress. I assume the role you want the capabilities to be copied over is called customer. You can adjust this.
add_role( 'distributor', 'Distributor', get_role( 'customer' )->capabilities );
The function accepts an array of capabilites. With get_role() you get an role object and access the capabilities of that role.
So we create a new role with the capabilities of an existing role.

with 2 separate Wordpress installs, how to achieve a new member signing up on one site and automatically be inserted into the other?

How can this situation be done?
Wordpress install at ABC.com.
Wordpress install at 123.com.
A new member signs up at abc.com, then instantly and automatically, the same user's email address and password be programmatically inserted into 123.com. After the abc.com sign up process is completed, the new user is redirected to log in at 123.com.
How to achieve that?
you can achieve your functionality using two tricks, firstly you have to connect your wordpress with second DB.
global $wpdb;
$newdb = new wpdb( 'USERNAME' , 'PASSWORD' , 'DATABASE NAME' , 'HOST' );
$newdb->show_errors();
Then you can use the user_register hook to get all data from user upon registration AND update them to new database as a user.
add_action( 'user_register', 'myplugin_registration_save', 10, 1 );
function myplugin_registration_save( $user_id ) {
//print_r($_POST); // to get all information of user
// Insert data in other database
$newdb->insert('wp_users', array(
'display_name' => $_POST['first_name'],
'user_email' => $_POST['user_email'],
'user_phone' => $_POST['user_phone'], // ... and so on
));
}
Haven't tried this code but it will work.

WordPress plugin combination

I'm using the pie registration plugin and I want to know how can I automatically define a role (in my case I want to define as subscriber) when people register and login. I want to do this because I want to restrict some zones of the website and redirect those zones for the register or login page. Since the free version of the plugin doesn't have restricted areas, I'm also using the plugin called members that does 50% of what I want. So I want to combine these 2 plugins' functionalities.
tldr: how can I give the role subscriber on pie-registration?
When you are going to register user you have assign it a role for ex.
if you are using wp_insert_user( $userdata ) function than you can assign user role in $userdata as follows.
$userdata = array(
'user_login' => 'login_name',
'user_pass' => 'pass',
'role' => 'role' // When creating an user, `user_pass` is expected.
);
There are some default roles in wordpress , you can also creates new roles by using following code.
<?php add_role( $role, $display_name, $capabilities ); ?>
And you can assign capabilities, so by using this two function you can achieve your goal.
You can read more about:wp_insert_user , add_role

in woocommerce how to set username as mobile number at registration

I am building a website using Woocommerce.By default the username is the customer username.I want to provide the users the feature to login through their phone number also. How can i do that? Please help!
This is possible. But I think not a good idea.. Imagine 2 users shares the same number.
But if you can manage to make the phone number unique, you can try something like this with WooCommerce login form.
add_filter('woocommerce_login_credentials','woocommerce_login_credentials', 10, 1);
function woocommerce_login_credentials($creds){
$username = trim( $_POST['username'] );
if (is_numeric($username)){ // assumes only numeric are allowed. You can do your own logic here.
$user = get_users(array(
'meta_query' => array(
array(
'key' => 'billing_phone',
'value' => $username,
'compare' => 'LIKE',
),
)
));
$creds['user_login'] = $user[0]->data->user_login;
}
return $creds;
}
This code is using the billing phone number. If this phone number belongs to a lot of users, only the first user that the query gets will be used. What this code do is, it gets the user_login of the user using the given phone number. Then use that as username to logged in. You can still use the username/email as login with this code.
This answer demonstrate only that it can be done. I won't advice you doing so.

Resources