How too redirect a non-logged-in or Login user from one page to another page

To redirect a non-logged-in user from one page to another page in WordPress, you can use the “template_redirect” action hook and the “wp_redirect” function to redirect the user to the desired page. Here’s an example code snippet that you can add to your functions.php file:

function redirect_dashboard_to_login() {
  if (is_page('dashboard') && !is_user_logged_in()) {
    wp_redirect('https://www.keenbech.com/login');
    exit;
  }
}
add_action('template_redirect', 'redirect_dashboard_to_login');

Note :- To redirect the “https://www.keenbech.com/dashboard” page to the “https://www.keenbech.com/login

 

In this example, the “is_page(‘dashboard’)” function checks if the user is on the “dashboard” page, and the “is_user_logged_in()” function checks if the user is not logged in. If both conditions are true, the user is redirected to the “login” page using the “wp_redirect” function. The “exit” function is used to stop the code execution after the redirect.

Note that you will need to replace “https://www.keenbech.com/login” with the actual URL of your login page.

Leave a Comment

Your email address will not be published. Required fields are marked *