Vous cherchez un moyen de rediriger les tentatives de connexion échouées vers n’importe quel endroit de votre site web ? Bien qu’il existe probablement un plugin pour cela, nous avons créé un extrait de code rapide que vous pouvez utiliser pour rediriger les tentatives de connexion échouées vers n’importe quel emplacement dans WordPress.
Instructions:
Tout ce que vous avez à faire est d’ajouter ce code dans le fichier functions.php de votre thème ou dans un plugin spécifique à votre site:
add_action( 'wp_login_failed', 'my_front_end_login_fail' ); function my_front_end_login_fail( $username ) { $referrer = $_SERVER['HTTP_REFERER']; if ( !empty($referrer) && !strstr($referrer,'wp-login') && !strstr($referrer,'wp-admin') ) { wp_redirect( $referrer . '?login=failed' ); exit; } }
Note : Si c’est la première fois que vous ajoutez des extraits de code dans WordPress, veuillez consulter notre guide sur la façon d’ajouter correctement des extraits de code dans WordPress, afin de ne pas casser accidentellement votre site.
Si vous avez aimé cet extrait de code, n’hésitez pas à consulter nos autres articles sur le site comme : 43 meilleurs thèmes de photographie pour WordPress et comment créer un formulaire de contact sur WordPress.
Hi,
I want to ask that after adding this codesnippet in wordpress i am unable to access my default wp-admin login page. Can you guide me how i can access my default wp-admin login?
Hi,I am using woocommerce account as my login page can you tell me a way i can block the user based on username when he fails to login 3 instead of blocking the ip address?
Try adding the Wordfence plugin
Is this snippet still working? Because my failed logins are still redirecting to wp-login.php
I am on WordPress 5.0.3
Thank you so much! I have been looking everywhere for something like this.
I do have 2 questions, is it possible to add a redirect for when the user leaves the fields empty? and how can I return a incorrect login error message?
thanks again!
Hi Eric, The following should help,
User controlled
http://wpsnipp.com/index.php/functions-php/user-controlled-optional-login-redirect/
Redirect back to referring page
http://wpsnipp.com/index.php/functions-php/redirect-back-to-referring-page-after-login/
Add a custom message to the login screen
http://wpsnipp.com/index.php/functions-php/add-a-custom-message-to-the-login-screen/
Thanks after so many snippet I tried and this one works. but now stuck with redirect on my page. I mean its redirecting to the last page but I want to notify my user that they have entered wrong userid or password. How to display that message? or may be redirect to my custom page.
Redirect to a custom page would work best I think,
Yeh I am fine with that but how to redirect? I am not master with php so don’t know where should I put my url for that page?
Line 5 is the redirect,
wp_redirect( $referrer . ‘?login=failed’ );
http://codex.wordpress.org/Function_Reference/wp_redirect
This page has more details on the function wp_reidrect.
Alright! done. Thanks a lot for your instant help.
One more thing why your last comment doesn’t have reply link?
Glad to hear you got things working Great stuff. In regards to your comments question this is just the level of nested comments I allow. Since eventually it would nest so far we could barely read comments anymore 🙂