Redirect away from login page - wordpress

I'm trying to find a way to prevent the login screen from showing up when user makes a mistake filling out the login form.
So, right now, I have the login form printed on a custom template. But if the users makes a mistake filling it out, it redirects them to the wordpress form to try again. I want to redirect away from that.
This is the code I had but it's not working.
add_action('login_form', 'redirect_invalid_login');
function redirect_invalid_login(){
global $error;
if($error)
header('Location: '.get_bloginfo('url').'/client-login/?message=6');
}
Now login_form is too late I believe because stuff already gets printed to the page. I haven't been able to find a hook that works. Is there a hook for this? Or perhaps a different way to do it?

Okay, so here is the solution:
add_action('login_redirect', 'redirect_login', 10, 3);
function redirect_login($redirect_to, $url, $user) {
if($user->errors['empty_password']){
wp_redirect(get_bloginfo('url').'/client-login/?message=6');
}
else if($user->errors['empty_username']){
wp_redirect(get_bloginfo('url').'/client-login/?message=7');
}
else if($user->errors['invalid_username']){
wp_redirect(get_bloginfo('url').'/client-login/?message=8');
}
else if($user->errors['incorrect_password']){
wp_redirect(get_bloginfo('url').'/client-login/?message=9');
}
else{
wp_redirect(get_bloginfo('url').'/client-login');
}
exit;
}

You can try this
add_action( 'wp_login_failed', 'redirect_invalid_login' );
function redirect_invalid_login() {
wp_redirect(get_bloginfo('url').'/client-login/?message=6');
exit;
}

Related

How to auto close wordpress page after sometime?

I have been struggling with this idea where i would want to auto close the woocommerce thank you page after 1 minute or so, but am unable to find a way out. The reason behind the same is because we are tracking it with Facebook Pixels. So, 1 views is counted as a sale.
So, we need to ensure, the customer can only view it once and not revisit it again. Is is possible?
You can redirect to any page after 1 minute from WooCommerce thank you page. I have tried below code to redirect to a home page after 60 seconds from thank you page.
add_action('template_redirect', 'custom_redirect_after_purchase');
function custom_redirect_after_purchase() {
global $wp;
if (is_checkout() && !empty($wp->query_vars['order-received'])) {
header( "refresh:60;url=".home_url() );
}
}
I hope this helps you to achieve what you want.
Another way to redirect using PHP and WordPress is like below you can try this
add_action('template_redirect', 'custom_redirect_after_purchase');
function custom_redirect_after_purchase() {
global $wp;
if (is_checkout() && !empty($wp->query_vars['order-received'])) {
sleep(60);
wp_redirect( home_url() ); exit;
}
}

add_filter not working on Wordpress custom login page

I'm creating a custom login page in wordpress. Everything is good so far but I want to add error handling to my page.
In default-filters.php, I added this:
add_filter('authenticate', 'wp_return_login_error', 40, 3);
In users.php, I added this:
function wp_return_login_error(){
if (is_wp_error($user)) {
$error = $user->get_error_message();
wp_redirect( get_permalink() . "/login/?login=failed&reason=" . $error);
exit;
}
}
No matter what, when you enter invalid/blank credentials, the user is simply redirected to the login screen without any indication of what went wrong. I tried adding debug messages in my function and it looks like it isn't being called.
What should I try?
Thanks in advance for your help.
The easiest way around this is to change the error message with this code in your functions.php file:
function login_error_override()
{
return 'Incorrect login details.';
}
add_filter('login_errors', 'login_error_override');

WordPress admin redirect conundrum

I feel stupid for trying this and not realising what would happen.
Anyway.
Using the wp_redirect function...
if( current_user_can('administrator') ) {
$url = "https://example.com";
wp_redirect( $url );
}
...I want anyone with the role of 'administrator' to be redirected to the homepage when they login. Seems easy enough.
But the problem is that any consequent attempt to go to WP admin (not just after they login, but any time) will result in the administrator being redirected to the homepage effectively 'locking' them out of WP admin.
So I guess my question is, is it possible to redirect an administrator to the homepage when they login, but only immediately after they login? And any consequent attempt to go to /wp-admin/ would let them in?
This code should solve your problem.You can add this code in functions.php.I tested and confirmed its working for me.
function admin_redirection_page() {
return 'https://example.com';
}
add_filter('login_redirect', 'admin_redirection_page');
I think you have to fire the above code in hook wp_login.
Example:
function your_function() {
$url = "https://example.com";
wp_redirect( $url );
die();
}
add_action('wp_login', 'your_function');
Also we have use die() whenever we want to redirect.
Thank You

Give access to only two (/home, /inbox) page for a particular user with specific role in wordpress

I want to give only two page (/home, /inbox) access to my user with role "Vendor", if user tries to access other pages than it will automatically redirect to "/inbox" page, I put the below code to achieve the functionality but after adding this to function.php, site again n again redirect and finally dies with message "Page isn't properly redirected". please suggest what is wrong with my tried code or any other solution.
function vendor_redirect() {
global $post;
if(current_user_can('Vendor') && !in_array($post->slug,array("home","inbox"))) {
wp_safe_redirect('/inbox');
}
}
add_action('template_redirect', 'vendor_redirect');
The main issue the way I tried to get the page slug, the correct way to get the slug is "$post->post_name". also I put exit after wp_safe_redirect as well because codex suggest that:
function vendor_redirect() {
global $post;
if(current_user_can('Vendor') && !in_array($post->post_name,array("home","inbox"))) {
wp_safe_redirect(get_permalink(get_page_by_path( 'inbox' )));
exit;
}
}
add_action('template_redirect', 'vendor_redirect');

WordPress redirect after successful form validation and submit

I have a custom page template with a form in page-report.php.
I do validation on it, so I need the action to lead to the same form, but I need to redirect to a different page on successful validation.
wp_redirect() is not working, because is spitting out the header() function after the output was started already.
if($_POST['report'])
{
if($validator->ValidateForm())
{
wp_redirect('http://thankyou') // redirect
}
}
I cannot use ob_start() and ob_flush() because the header is not included in this page template.
I tried to put a function in functions.php :
add_action('get_header','redirect_to');
function redirect_to($page){
if($page)
{
wp_redirect('http://www.google.com');
}
}
But that works only if I don't have the conditional if().
If I use it, the wp_redirect() is being spat out after the output was started.
What is my best approach to do this?
Thanks.
I think you have to use the save_post hook:
do_action('save_post', 'custom_add_save');
function custom_add_save($postID){
// called after a post or page is saved
if($_POST['report']) {
if($validator->ValidateForm())
{
wp_redirect('http://thankyou') // redirect
}
}
Also you could just try using a plugin instead of your own code...Gravity Forms and Contact form 7 both work well.
}
I got it...
Since I was doing everything from inside an admin page, the header was fired up before the wp_redirect() as it was explained in the question.
So I ended up making a new function at the top:
add_action('admin_init','redirect_to');
function redirect_to()
{
if ( isset($_REQUEST['action']) && 'adduser' == $_REQUEST['action'] ) {
wp_redirect($redirect);
die();
}
}
}
That is making sure that the redirect_to() function will be fired up before the header (on admin_init). Maybe not the most elegant solution, but it works perfect.
So in the case anybody is looking for "Redirect after post" in wordpress, this is how you do it:
wp_redirect($_SERVER['REQUEST_URI']);
Try this
if( $_POST['report'] ) {
if( $validator->ValidateForm() ) {
header( 'Location: http://thankyou' ) ;
}
}

Resources