execute a function when password changed in Wordpress - wordpress

For increase security i'm looking for a way to run a custom function when an Admin user change it's password in Wordpress CMS.
please help me. thank you.

WordPress sends an email to the admin's email when a user resets their password.
To get a notification when a user changes their password you could hook into the profile_update action which is fired when a user's profile is updated.
When the action is fired WordPress has already validated and updated the user's details we only need to check if the user submitted a password with the request, if it was submitted then the user's password has changed.
function my_profile_update( $user_id ) {
if ( ! isset( $_POST['pass1'] ) || '' == $_POST['pass1'] ) {
return;
}
elseif(!$_POST['pass1'] === $_POST['pass2']){
return;
}
// password changed...
}
add_action( 'profile_update', 'my_profile_update' );

Related

Wordpress custom login errors based on whether or not an advanced custom field is set true for that user

I have kind of a unique issue. I have migrated a website and rebuilt it on wordpress. Some of the users that were migrated over the passwords were not migrated with them. So for all the old users I added an advanced custom field named "password_reset" and set it to true for all the older users.
What I am trying to do is show a custom message for these users that says something like "we have updated our website please rest your password with a link to reset".
I have added the below code to a function in the functions.php file
//if migrated user needs to reset password
$username = $_POST['username'];
if (username_exists( $username ) && get_field( 'password_reset', 'user_'.$uid ) ) {
$error= 'Please reset your password. To reset your password click here.';
}
return $error;
since the user isn't logged in quite yet when they are to recieve this error, I am trying to use the username_exists(username). Basically I need to identify is the user name exists already and if that username has the acf field "password_reset" checked. So far I have had no luck, any help would be much appreciated.
UPDATE: here is my lates version: the messages for invalid username and incorrect password are working, Just can't get it to work with the usernames that hold the acf value
function my_custom_error_messages() {
global $errors;
$err_codes = $errors->get_error_codes();
// Invalid username.
if ( in_array( 'invalid_username', $err_codes ) ) {
$error = '<strong>ERROR</strong>: Invalid username.';
}
// Incorrect password.
if ( in_array( 'incorrect_password', $err_codes ) ) {
$error = '<strong>ERROR</strong>: The password you entered is incorrect.';
if (username_exists( $username ) && get_field( 'password_reset', 'user_'.$uid ) ) {
$error= 'Please reset your password. To reset your password click here.';
}
}
return $error;
}
add_filter( 'login_errors', 'my_custom_error_messages');
I believe the issue is that the username is not passed to the login_errors filter. The only data available within that filter is the error message that is passed (no definitive data about user accounts at all).
I have found a different reference that may shed some light on a way to provide a customized error message without using that filter. Try using the wp_authenticate_user filter, instead:
WordPress codex reference
https://codex.wordpress.org/Plugin_API/Filter_Reference/wp_authenticate_user
Reference for application of code
https://backups.nl/internet/wordpress-revealing-username-login-trial-error/

Disable WooCommerce email notification for specific customer group or user role

I'm looking for a solution where we can disable woocommerce sending email notification when an order is done by a client from a specific customer group (user role).
I found answer about a situation what prevent sending email for a specific product-ID.
Disable WooCommerce email notification for specific product.
Maybe this could be possible for our 'problem' too?
Kind regards,
Kees
You can use the hook for any email and inside the callback function you can check if the user has a specific role
function change_new_order_email_recipient( $recipient, $order ) {
global $woocommerce;
$uid = $order->get_user_id();
$user_meta=get_userdata($uid);
$user_roles=$user_meta->roles;
if(in_array('customer', $user_roles)){ // Prevent email if user role is customer
$recipient ='';
}
return $recipient;
}
add_filter('woocommerce_email_recipient_customer_completed_order', 'change_new_order_email_recipient', 1, 2);
I have checked the code rapidly and is working

Disable auto login upon registration in Wordpress

[update1] I am using the ClassiCraft theme and I have no idea where to customize the login and register forms
[update2] I know that the registration process does not go through wp_authenticate because I redefined it inside a plugin of mine
I am quite new in the Wordpress world (actually just got my hands on it for the first time yesterday) and I am having some difficulties finishing up a little project I am working on.
The project is rather simple (or so I thought) and consists in adding a confirmation link to email received upon registration in order to validate the email address provided to prevent using fake emails that the registrar does not even own.
I am about all done except that once I hit the register button it leads to log in the freshly created user.
I googled stuff like "wp disable auto login on registration" and whatnot but I have not been able to find anything that worked. I even tested a few plugins supposed to be doing exactly what I need but none of them worked.
Also, I am not using any plugins for the registration/login forms and it appears that the code in the wp-login.php file is actually not even used...
Would anyone have an idea? Thanks
Okay, so without an access to the theme, i can't really answer you.
But i can tell you what I would try.
1. Add action on user_register hook, to add a post meta that will be useful to check if user has confirm his email.
add_action( 'user_register', 'add_has_confirm_email_user_meta');
function add_has_confirm_email_user_meta( $user_id ) {
update_user_meta( $user_id, 'has_confirm_email', 0 );
}
2. Prevent the user from log in automatically after registration.
Here i can't tell you the hook that will works for you. For example, the hook for the wordpress registration is user_register, but if you have woocommerce, the hook I will use, would be woocommerce_registration_redirect. So try to find what hook is available after the registration with your theme.
In all case, the code in the function would be something like :
function custom_registration_redirect() {
// Log out the user
wp_logout();
// The login url could be an other, with woocommerce for example it is : get_permalink(get_option('woocommerce_myaccount_page_id')
$login_url = wp_login_url();
// Redirect on it
wp_redirect( $login_url);
exit;
}
It will also be necessary, to add a message on this page to alert the user, that he will receive an email to confirm his account.
3. Prevent user from login when he submit the log in form
Add action on wp_login hook to achieve that.
add_action('wp_login', 'prevent_user_from_login', 10, 2);
function prevent_user_from_login($user_login, $user = null ) {
if ( !$user ) {
$user = get_user_by('login', $user_login);
}
if ( !$user ) {
// not logged in
return;
}
// Get user meta
$has_confirm_email = get_user_meta( $user->ID, 'has_confirm_email', true );
if ( $has_confirm_email == '0' ) {
// Clear cookies, a.k.a log user out
wp_clear_auth_cookie();
$login_url = wp_login_url();
$login_url = add_query_arg( 'has_confirm_email', '0', $login_url);
wp_redirect( $login_url );
exit;
}
}
4. Add message on log in page if we get the has_confirm_email to 0
add_filter('login_message', 'has_not_confirm_email_login_message');
function has_not_confirm_email_login_message($message) {
if ( isset( $_GET['has_confirm_email'] ) && $_GET['has_confirm_email'] == 0 ) {
$message = '<div id="login_error">You have not confirmed your email.</div>';
}
return $message;
}
5. Send the email with a link to confirm his email.
You will need to generate a token to add to the url.
For the hook to change the default email sent by Wordpress, you can use wp_new_user_notification_email that is available since the 4.9 of Wordpress.
In the function itself you could do something like :
function wp_new_user_notification_email( $wp_new_user_notification_email, $user, $blogname) {
// Generate the token (there is other function available with php 7, but this one works great)
$token = bin2hex(openssl_random_pseudo_bytes(16));
// Add the token to the user
update_user_meta( $user->id, 'confirm_email_token', $token );
// Get your login url
$log_in_url = wp_login_url();
// Add user id and token to the url
$url = add_query_arg(
array(
'token' => $token,
'user_id' => $user->id
),
$log_in_url
);
//
$wp_new_user_notification_email['subject'] = 'Welcome on our website, please confirm your email';
$wp_new_user_notification_email['message'] = 'Blablabla... the url to confirm is: '. $url;
return $wp_new_user_notification_email;
}
6. Hook on the login page to check the $_GET, looking for user_id and token.
Here we check the token and the user. If everything is okay, update the user meta has_confirm_email to 1, so the user can connect, and add a message : "Your email has been confirmed, you can now log in"
add_action( 'login_init', 'custom_login_init');
function custom_login_init(){
if(!empty($_GET['token']) && !empty($_GET['user_id'])) {
if(get_the_author_meta( 'confirm_email_token', $_GET['user_id']) === $_GET['token']) {
// Set the has_confirm_email to 1 so the user can now log in
update_user_meta( $user_id, 'has_confirm_email', 1);
update_user_meta( $user_id, 'confirm_email_token', '');
echo 'Your email has been confirmed, you can now log in';
}
}
}
7. Time for thinking
Okay, after all of his, i'm gonna think a little, and read what i have tell you, to check if there is no mistake ^^. Tell me if you need more explanations.
I think this is a good start for you, and if you find the right hooks, you will achieve this rapidly.
Be careful on some hooks that i have used, because your theme may have use a custom registration or something.
Here is what I did:
added a column in the table wp_users to receive the email confirmation code
built a plugin (details here) called user-emails that allows me to bypass the first email sent upon registration by redefining the function wp_new_user_notification (in which I generate the confirmation code, add it to the user in the DB and send a confirmation email of my own sauce)
redefined the wp_authenticate function inside the same plugin user-emails to allow me to check if the email has been confirmed (column value not null)
created a page for the confirmation with the email and code passed to it that, in case of success, display a message and a link to the home page in order to login
finally got my hands on that one tiny line of code responsible for the auto login after registration located in the page user_auth.php inside the theme folder itself (that file also contains the layout for the login and registration form)
wp_set_auth_cookie( $user_id, true, $secure_cookie );
made sure to display a message after registration informing the user to check his email for the confirmation email

How to get user id in wordpress after registered?

I am working on sending OTP to user and verify it.
I am using wordpress and I created mobile number field in default registration form and when user submit registration form , then he will be receive otp which we generated through an algorithm and used sms API for sending sms.
After submit the registration form , he will redirect to one page in that page i want to get registered users details like surname, first name.
So that I can verify the otp for that user.
Other thing is how can a user will be registered only after OTP verification.
The user_register hook is fired right after a successful registration.
add_action( 'user_register', 'my_theme_registration_do_stuff', 10, 1 );
function my_theme_registration_do_stuff( $user_id ) {
// the only native way in WP to 'deactivate' a user is to set the role to 'none'. Read up on the implications of this and decide if this will suffice. If not, then you'll need to create some sort of user_meta to use for active/inactive user
$u = new WP_User( $user_id );
// Remove role
$u->remove_role( 'subscriber' ); //or whatever your site's default role is
}
Now the user will continue to the next page.
Once the user gets the SMS message with the code and he/she enters the code into some form on your website:
if(the code entered is correct){
$user = new WP_User( get_current_user_id() );
$user->add_role( 'subscriber' );
}

reset wordpress username and password via FTP

I have a website that is hosted by godaddy.My website made in WordPress and I want access my word press websites admin panel and I do not have username and password but I have access to FTP of my website.
Kindly tell me if there is any way I can reset or know previous username and password of my word press admin panel?
Many Thanks.
Mark
You may also add following code in functions.php to create a new admin user
function wpb_admin_account(){
$user = 'Username';
$pass = 'Password';
$email = 'email#domain.com';
if ( !username_exists( $user ) && !email_exists( $email ) ) {
$user_id = wp_create_user( $user, $pass, $email );
$user = new WP_User( $user_id );
$user->set_role( 'administrator' );
} }
add_action('init','wpb_admin_account');
I had implemented this solution when I was stuck in same issue.
I'm not sure of a way you can reset your password or check your username via FTP. You need to access your database for that information. I would Login to your cpanel at godaddy and go into phpMyAdmin. Login in and find your Wordpress database, and then look at the table called wp_users. This will give you a list of all the users and their information. The password will be encrypted, but you now be able to go to the Wordpress login page and click forgot password and rest one of the users now that you know the right username or the email associated to that user.
Step by step tutorial here, VERY EASY!
https://codex.wordpress.org/Resetting_Your_Password#Through_FTP
To reset your password using ftp
download function.php file of your activated theme.
put this function
save and upload
You can also use this function to create new user.
$user_id = wp_create_user($username, $password); //it will return user id
$user = new WP_User($user_id); //get the user
$user>set_role('administrator'); //set user_role to administrator
Enjoy !!!

Resources