I'm trying to determine how I can redirect users upon logout, to a URL defined by their role. Simply put, I want to redirect admins (as well as editors) that logout to a different URL, than subscribers / privileged users.
I'm using the following code to redirect users at logout right now, but this redirects everyone. Any insight as to how I can have a different redirect based on their account role, would be great!
/**
* Redirect to custom login page after the user has been logged out.
*/
public function redirect_after_logout() {
$redirect_url = home_url( 'member-login?logged_out=true' );
wp_safe_redirect( $redirect_url );
exit;
}
add_action( 'wp_logout', array( $this, 'redirect_after_logout' ) );
Thanks!
Please check the doucumentation https://developer.wordpress.org/reference/functions/current_user_can/ how to check if current user has the specified capability.
Here is a sample:
if (!current_user_can('edit_posts') && !is_admin()) {
$redirect_url = site_url();
} else {
$redirect_url = '/wp-login.php';
}
function wpdocs_redirect_after_logout()
{
global $redirect_url;
wp_safe_redirect($redirect_url);
exit;
}
add_action('wp_logout', 'wpdocs_redirect_after_logout');
Im not sure didnt test but you can use user roles with if else statement
function redirect_after_logout() {
if (!current_user_can('manage_options')) {
$url = '/';
} else {
$url = 'member-login?logged_out=true';
}
$redirect_url = home_url( $url );
wp_safe_redirect( $redirect_url );
exit;
}
add_action( 'wp_logout', array( $this, 'redirect_after_logout' ) );
Related
When users click a purchase button without login, following code opens a login popup (it works well).
My problem is, I need to redirect to the checkout page (www.mywebsite.com/my-checkout) after the login (since the user logged in to checkout).
At the moment, when users click the purchase button, it goes to the home page and opens a login popup, and stays on the home page after login.
Would you please let me know how to adjust the following code?
add_action('template_redirect','check_if_logged_in');
function check_if_logged_in()
{
$pageid = get_option( 'woocommerce_checkout_page_id' );
if(!is_user_logged_in() && is_page($pageid))
{
$url = add_query_arg(
'redirect_to',
get_permalink($pagid),
site_url('/#login')
);
wp_redirect($url);
exit;
}
if(is_user_logged_in())
{
if(is_page(get_option( 'woocommerce_myaccount_page_id' )))
{
$redirect = $_GET['redirect_to'];
if (isset($redirect)) {
echo '<script>window.location.href = "'.$redirect.'";</script>';
}
}
}
}
Thank you.
You can use woocommerce_login_redirect hook.
function wc_redirect_login( $redirect, $user ) {
$redirect_page_id = url_to_postid( $redirect );
$checkout_page_id = wc_get_page_id( 'checkout' );
if( $redirect_page_id == $checkout_page_id ) {
return $redirect;
}
return wc_get_page_permalink( 'shop' );
}
add_filter( 'woocommerce_login_redirect', 'wc_redirect_login' );`
You can also try WooCommerce Login Redirect plugin to customize redirects.
I am using woocommerce and I have created a login and register page. I am not using any plugin for this.
My issue is, I have to check and redirect to log in before checkout I refer to this code(WooCommerce check and redirect to login before checkout)
I am using the below code
add_action('template_redirect', 'check_if_logged_in');
function check_if_logged_in() {
$pageid = 10; // your checkout page id
if (!is_user_logged_in() && is_page($pageid)) {
$url = add_query_arg(
'redirect_to',
get_permalink($pagid),
site_url('/my-account/') // your my acount url
);
wp_redirect($url);
exit;
}
}
but when I click Proceed to checkout then I am getting the URL like
http://examle.com/my-account/?redirect_to=http://example.com/index.php/checkout/
You could do it in two steps.
If the user goes to checkout and is not logged in, he is redirected to the my-account page for login or registration.
// if the user is not logged in, return him to login before checkout
add_action('template_redirect', 'check_if_logged_in');
function check_if_logged_in() {
if ( ! is_user_logged_in() && is_checkout() ) {
$url = site_url('/my-account/');
wp_redirect( $url );
exit;
}
}
After login or registration, if the cart is not empty, the user is redirected to checkout.
// redirect to checkout after login (or registration)
add_filter( 'woocommerce_registration_redirect', 'redirect_after_login_or_registration_to_checkout_page' );
add_filter( 'woocommerce_login_redirect', 'redirect_after_login_or_registration_to_checkout_page' );
function redirect_after_login_or_registration_to_checkout_page() {
// only if the cart is not empty
if ( ! WC()->cart->is_empty() ) {
return wc_get_page_permalink( 'checkout' );
} else {
return home_url();
}
}
The code has been tested and works. Add it to your theme's functions.php.
I'm using this functions.php code so when my clients want to check out they will redirect to Sign up or log in before they checkout, I tried this method below but after all it does not redirect to Checkout page but to My Account, any idea on how to fix the code so I will be redirected to Checkout and not to My Account.
add_action('template_redirect','check_if_logged_in');
function check_if_logged_in()
{
$pageid = 68; // your checkout page id
if(!is_user_logged_in() && is_page($pageid))
{
$url = add_query_arg(
'redirect_to',
get_permalink($pagid),
site_url('/my-account/') // your my acount url
);
wp_redirect($url);
exit;
}
}
Try using this filter: woocommerce_registration_redirect
function woocommerce_register_redirect( $redirect ) {
return wc_get_page_permalink( 'cart' );
}
add_filter( 'woocommerce_registration_redirect', 'woocommerce_register_redirect' );
I would like to do that only administrator(role) allow to login with wp-admin not any other role, other uses like(editor,author) are login from front end and it's working fine but i would like only administrator can login through wp-admin.
I have used ultimate member plug in for front end login.
Ultimate Plugin link
also i have used below code for access wp-admin only for administrator(role) but it's not working.
<?php
function restrict_admin(){
//if not administrator, kill WordPress execution and provide a message
if( !current_user_can('administrator') ) {
wp_die( __('You are not allowed to access this part of the site') );
}
}
add_action( 'admin_init', 'restrict_admin', 1 );
?>
Thank you guys for your help, I am answering my own question hope this will help others too.
I have referred this link https://developer.wordpress.org/reference/functions/wp_authenticate/
I have solved this with hook wp_authenticate
add_action( 'wp_authenticate' , 'check_custom_authentication' );
function check_custom_authentication ( $username ) {
$username;
$user = new WP_User($username);
$user_role_member=$user->roles[0];
if($user_role_member == 'author' || $user_role_member == 'editor'){
session_destroy();
wp_redirect( home_url() );
exit;
}
}
add_action( 'admin_init', 'redirect_none_admin' );
function redirect_none_admin(){
if(is_admin() && current_user_can(activate_plugins)){
//...
}else{
wp_redirect(home_url());
}
}
i tested this one and it worked i think it is simple and easy
you can find Roles and Capabilities here
If anyone will need the same here is code that allows only administrators, authors and editors to login using /wp-login.php
//------------- This website is read only - so only staff can log in -------------
add_filter( 'authenticate', 'myplugin_auth_signon', 30, 3 );
function myplugin_auth_signon( $user, $username, $password ) {
$user_role_member=$user->roles[0];
if(!in_array($user_role_member,array('administrator','author','editor'))){
wp_logout();
return new WP_Error( 'broke', __( "This website is read only for regular users", "your_wp_domain_name" ) );
exit;
}else{
return $user;
}
}
//------------- this website is read only - so staff can log in ------------------
You can add\remove roles in the array above array('administrator','author','editor')
Try this
add_action( 'admin_init', 'redirect_non_admin_users' );
/**
* Redirect non-admin users to home page
*
* This function is attached to the 'admin_init' action hook.
*/
function redirect_non_admin_users() {
if ( is_admin() !== false) {
wp_redirect( home_url() );
exit;
}
}
Here is the code in my functions.php that redirects users (to their new "Sample Page") when they log in:
function admin_default_page() {
return '/wp-admin/post.php?post=2&action=edit';
}
add_filter('login_redirect', 'admin_default_page');
The problem is that the url to which we redirect is missing the users site url.
For example, if a user creates a site "Bobs Trucks", it redirects them to
example.com/wp-admin/post.php?post=2&action=edit (does not work)
instead of
example.com/bobstrucks/wp-admin/post.php?post=2&action=edit
I've tried all the WordPress functions to get the full url needed (example.com/bobstrucs/url-to-redirect-to) but have not succeeded.
I'm trying a rather hackish solution where I get the user ID form the logged in user, and get all the blogs the user has (= just one, "Bobs Trucks"):
function admin_default_page() {
global $current_user;
get_currentuserinfo();
$blogs = get_blogs_of_user( $current_user->ID);
foreach($blogs as $blog) { $userblog = $blog->siteurl; }
return $userblog .'/wp-admin/post.php?post=2&action=edit';
}
add_filter('login_redirect', 'admin_default_page');
But this one has the problem of not working the first time user logs in.
Thank you very much!
Try something like this:
function admin_default_page() {
if ( is_user_logged_in() && is_admin() ) {
$blog_id = get_current_blog_id();
$blog = get_blog_details( array( 'blog_id' => $blog_id ) );
$location = '/' . $blog->blogname . '/wp-admin/post.php?post=2&action=edit';
wp_redirect( $location );
exit;
}
}
add_filter( 'login_redirect', 'admin_default_page' );
Refs:
http://codex.wordpress.org/Function_Reference/get_blog_details
http://codex.wordpress.org/Function_Reference/wp_redirect