Woocommerce/Wordpress - Redirect User Login to Homepage

泪湿孤枕 提交于 2021-02-07 03:11:03

问题


I have searched for the answer to this, used plugins and still nothing works. I would like users of my site to be redirected to the home page after they login/register.

Currently, the user logs in and is redirected to the my account page.

Woocommerce provides this code, but it failed to work for me:

/*
 * goes in theme functions.php or a custom plugin
 *
 * By default login goes to my account
 **/
add_filter('woocommerce_login_widget_redirect', 'custom_login_redirect');

function custom_login_redirect( $redirect_to ) {
     $redirect_to = 'http://anypage.com';
}

I have also attempted to use the Peter's Redirect plugin but it does not work since woocommerce bypasses wp-login.php.

Thoughts?


回答1:


Use this code on functions.php

add_filter('woocommerce_login_redirect', 'wc_login_redirect');
function wc_login_redirect( $redirect_to ) {
     $redirect_to = 'https://www.google.co.in/';
     return $redirect_to;
}



回答2:


You can download http://wordpress.org/extend/plugins/peters-login-redirect/ and replace this in the widget-login.php

<input type="hidden" name="redirect_to" class="redirect_to" value="<?php echo $redirect_to; ?>

with

<input type="hidden" name="redirect_to" class="redirect_to" value="<?php echo site_url('/wp-content/plugins/peters-login-redirect/wplogin_redirect_control.php/'); ?>" />

That way you can use peters login redirect to redirect globally and specific users using the woocommerce login widget.

Make sure you change "Use external redirect file. Set this to "Yes" if you are using a plugin such as Gigya that bypasses the regular WordPress redirect process (and allows only one fixed redirect URL). Then, set the redirect URL to %s" in the settings to "YES".

Hope this helps.




回答3:


That fix they provide only works if you are utilizing the login widget. All you need to do it redirect the user after logging in to your home page is to add this to your login form:

<input type="hidden" name="redirect" value="<?php echo get_home_url(); ?>" />



回答4:


There has been a filter recently added to allow you to redirect after registration. Doing a redirect on login is as simple as Tomanow mentions above (putting it in form-login.php). Here is a link to the filter and some instructions for handling this on the registration form.

https://github.com/woothemes/woocommerce/commit/014e31952828377bf7a1ebf4e812a43d0bcefa67#commitcomment-3351995




回答5:


Use this code on functions.php

add_action('wp_logout','go_home');
function go_home(){
  wp_redirect( home_url() );
  exit();
}



回答6:


Do this:

Go to admin > Woocommerce > Settings > General

Find "Customer Accounts" under "Cart, Checkout and Accounts"

Uncheck "Prevent customers from accessing WordPress admin"

Save Changes and test!




回答7:


You can set redirect according to user role. Given bellow the code for that redirection and I have developed a WooCommerce Login or Register Redirect plugin for entry level user or nor technical or no-programmer.

 //Redirect users to custom URL based on their role after login

function wp_woo_custom_redirect( $redirect, $user ) {

// Get the first of all the roles assigned to the user
$role = $user->roles[0];
$dashboard = admin_url();
$myaccount = get_permalink( wc_get_page_id( 'my-account' ) );

if( $role == 'administrator' ) {

    //Redirect administrators to the dashboard
    $admin_redirect = get_option('admin_redirect');
    $redirect = $admin_redirect;
} elseif ( $role == 'shop-manager' ) {

    //Redirect shop managers to the dashboard
    $shop_manager_redirect = get_option('shop_manager_redirect');
    $redirect = $shop_manager_redirect;
} elseif ( $role == 'customer' || $role == 'subscriber' ) {

    //Redirect customers and subscribers to the "My Account" page
    $customer_redirect = get_option('customer_redirect');
    $redirect = $customer_redirect;
} else {

    //Redirect any other role to the previous visited page or, if not available, to the home
    $redirect = wp_get_referer() ? wp_get_referer() : home_url();
}
return $redirect;
}
add_filter( 'woocommerce_login_redirect', 'wp_woo_custom_redirect', 10, 2 );



回答8:


Also include this for standard WordPress login form logins that use wp-login.php:

add_filter('login_redirect', 'custom_login_redirect');

I use this filter and the one you have given, against the same filter function, and it catches the two places where a user may log in (WP and WC).

Also just noticed the obvious problem. You have this in your filter function:

$redirect_to = 'http://anypage.com';

but you need this:

return 'http://anypage.com';

$return_url is passed in from previous filters, so you can inspect it and decide whether to change it.

Edit:

Actually, I need to correct some of this. It looks like the woocommerce_login_widget_redirect filter is used to set the redirect parameter in the widget login form. This means it only fires when the user is not logged in and the widget login form is presented. It cannot be used to decide where to send the user after they have logged in.

The WP login_redirect filter fires after the user has logged in, and can modify any redirect_to URL sent with the login form to enable you to redirect the user to some other page.

In addition, the WooCommerce login widget does not handle the logins that you would assume it does. The login process is handled via AJAX in woocommerce-ajax.php You will see in there that the redirect URL is not passed through the woocommerce_login_widget_redirect filter and so cannot be modified. I'm going to raised this as a pull request with WooCommerce, because I believe it should be filtered.

https://github.com/woothemes/woocommerce/pull/2508



来源:https://stackoverflow.com/questions/11958422/woocommerce-wordpress-redirect-user-login-to-homepage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!