Ryso
Ryso

Reputation: 55

Wordpress redirect user if not logged in

I'm using the following code in functions.php to redirect users who are not logged in, excluding if you are in wp-login.php.

if(!is_user_logged_in() && $pagenow != 'wp-login.php') {
wp_redirect( 'http://signup.mysite.com', 302 );
}

However, I've installed a shopping cart plugin that uses a different login page and basically redirects mysite.com/wp-login.php to mysite.com/account/login.

Is there some code I can use to exclude url paths from redirecting, perhaps something to replace $pagenow != 'wp-login.php'?

Upvotes: 5

Views: 35436

Answers (4)

krunal sojitra
krunal sojitra

Reputation: 385

 <?php     
if (!is_user_logged_in()) {
?> 
<style>
.lor1{ display:none;}
.lor1.kun{ display: block;}
.lor1.user_not_login{ display: block;}
</style>
<?php }  ?>
  1. main div(.lor1) means comman div start after header and finesh before footer
  2. Add new class (.lor1.kun) in main div when user loging this main class display: block;
  3. make new html Ex. display mess loging frist one (.lor1.user_not_login)

ADD this code in header.php

Upvotes: 0

Jacob King
Jacob King

Reputation: 196

Open Functions.php...

Paste:

// Redirect users who arent logged in...
function login_redirect() {

    // Current Page
    global $pagenow;

    // Check to see if user in not logged in and not on the login page
    if(!is_user_logged_in() && $pagenow != 'wp-login.php')
          // If user is, Redirect to Login form.
          auth_redirect();
}
// add the block of code above to the WordPress template
add_action( 'wp', 'login_redirect' );

hope this helps :)

Upvotes: 12

madhavaji
madhavaji

Reputation: 121

How about this..

//
//  Re-direct not-logged-in users to holding page
//
if(!is_user_logged_in() && curPageURL() != 'http://mysite.com/wp-login.php') {
    wp_redirect( 'http://signup.mysite.com', 302 );
    exit;
}

//
//  Get current page URL
//
function curPageURL() {

    $pageURL = 'http';
    if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}

    $pageURL .= "://";
    if ($_SERVER["SERVER_PORT"] != "80") {
        $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
    } else {
        $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
    }

    return $pageURL;
}

Upvotes: 12

Dominic P
Dominic P

Reputation: 2404

You could experiment with the WordPress is_page( 'login' ) or maybe is_page( 'account/login' ) conditionals.

Upvotes: 0

Related Questions