0

I have specific templates to request login.

I tried these three versions on functions.php file.

#1

This is not working. I had to do add_action( 'wp_head', 'template_login_permit' );

Because I put template directory? if I put 'wp_head', auth_redirect(); is not working.

function template_login_permit() {
if ( 
    is_page_template('templates/tpl-aa.php') || 
    is_page_template('templates/tpl-bb.php') ||  
    is_page_template('templates/tpl-cc.php') ||  
    is_page_template('templates/tpl-dd.php') ||  
    is_page_template('templates/tpl-ee.php') ||  
    is_page('shop')
    ) { 

    //echo "ok";
    //auth_redirect();
         if ( !is_user_logged_in() ) {
             //wp_redirect('/login');
             auth_redirect();
             echo "ok";
         }
}

}
   add_action( 'init', 'template_login_permit' );

#2

add_action( 'wp', 'login_redirect' ); wp are for all of templates, right? then how can I setup for only specific templates?

    // 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();
      echo "";
 }
 // add the block of code above to the WordPress template
  add_action( 'wp', 'login_redirect' );

#3

This for just testing for custom Login page I have.

add_action('init','possibly_redirect');

function possibly_redirect(){
 global $pagenow;
if( 'wp-login.php' == $pagenow ) {
 wp_redirect('/login');
 exit();
   }
  }

Is there any way to request "login"(I have custom login page) for specific templates?

2

Your first function is just on the wrong hook. init is too soon for knowing which page template/page you are on because the query is not set up yet. The best hook for redirecting is usually template_direct. The following works for me:

function template_login_permit() {

    if (
        is_page_template('templates/tpl-aa.php') ||
        is_page_template('templates/tpl-bb.php') ||
        is_page_template('templates/tpl-cc.php') ||
        is_page_template('templates/tpl-dd.php') ||
        is_page_template('templates/tpl-ee.php') ||
        is_page('shop')
        ) {


            if ( !is_user_logged_in() ) {
                auth_redirect();
            }


    }

}
add_action( 'template_redirect', 'template_login_permit' );
| improve this answer | |
  • Thanks so much. I have a short question. I have custom login do you think it's good to put wp_redirect('/login'); instead of auth_redirect? I haven't finished the login page. so I can't really check now. If it is ok, It should be also redirect to previous page. anyway, thanks for ur time – pulla Nov 1 '13 at 15:04
  • wp_redirect() is fine, but redirecting back is another question. You can research that and then post another question if you need. – helgatheviking Nov 1 '13 at 15:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.