Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

So I made a members only site with wordpress by redirecting everything to the homepage and adding a registration/login page to my homepage.

This was added as a plugin:

/* Change Login URL */
add_filter('login_url', 'change_login_url');
function change_login_url() {
    return home_url();
}
/* Redirect User */
add_action('template_redirect', 'user_redirect');
function user_redirect(){

    if ( trim( $_SERVER['REQUEST_URI'], '/' ) != '' && ! is_user_logged_in() ) {
        wp_redirect( home_url(), 301 );
        exit;
    }
}
/* Failed Login */
add_filter('login_errors', 'failed_login');
function failed_login() {
    return 'The login information that you have entered is incorrect.';
}
/* Registration Redirect */
add_filter( 'registration_redirect', 'registration_redirect' );
function registration_redirect(){
    return home_url();
}

Then added a shortcode in:

/*-------------------------------------------------------*/
/* Logged in/out User Shortcode from Jack Lamb
/*-------------------------------------------------------*/
// Security check to see if someone is accessing this file directly
if(preg_match("#^logged-in-user-shortcode.php#", basename($_SERVER['PHP_SELF']))) exit();

// [loggedin]content[/loggedin] returns content only to logged in users
function wpfc_logged_in( $atts, $content = null ) {
  if (is_user_logged_in() )
      {
        return do_shortcode($content);
      }
}

add_shortcode('loggedin', 'wpfc_logged_in');

// [loggedout]content[/loggedout] returns content only to logged out users
function wpfc_logged_out( $atts, $content = null ) {
  if (is_user_logged_in() )
      {}
    else return do_shortcode($content);
}

add_shortcode('loggedout', 'wpfc_logged_out');

The created a homepage with basically this:

[loggedout][registration_form][login_form][/loggedout]

[loggedin]Special Members area[/loggedin]

Which works great! Only problem is now I have a page that I do not want to redirect to the register page and secure, call it 'books' (http://www.mysite.com/books). how would I create a passthrough in mt 'user_redirect' function?

/* Redirect User */
add_action('template_redirect', 'user_redirect');
function user_redirect(){

    if ( trim( $_SERVER['REQUEST_URI'], '/' ) == 'books' () ) {
        // do_nothing or act normal
    } elseif ( trim( $_SERVER['REQUEST_URI'], '/' ) != '' && ! is_user_logged_in() ) {
        wp_redirect( home_url(), 301 );
        exit;
    }
}

or is there another method i should use..

share|improve this question
    
Normally you would use your template's header.php file to do any filtering( for authentication etc... ). That doesn't work for you? –  sri Apr 9 at 16:19
    
I messed up and said functions.php, I actually wrote this as a plugin. –  Rizzo Apr 9 at 16:23
    
Then please update your question, if you haven't done that already... –  sri Apr 9 at 16:32
    
done, thanks for the clarification. –  Rizzo Apr 9 at 16:49

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.