How To Redirect A User To Another Web Page If They Are Not Logged In Using WordPress

here’s an example of how to redirect a user to another web page if they are not logged in using WordPress:

<?php
  // Check if the user is not logged in
  if (!is_user_logged_in()) {
    // Redirect to another web page
    header('Location: https://example.com/login');
    exit();
  }
?>

In this code, we’re using the is_user_logged_in() function provided by WordPress to check if the current user is logged in. If the user is not logged in, we use the header() function to send a Location header to the browser, which will redirect the user to the login page at https://example.com/login. The exit() function is called to ensure that no further code is executed after the redirect.

Note that this code should be added at the beginning of the PHP file, before any output is sent to the browser. If you try to use header() after any output has been sent, you’ll get an error.

Leave a Reply

Proudly powered by WordPress | Theme: Code Blog by Crimson Themes.