admin管理员组

文章数量:1405864

With the following function :

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

Which action should be used?

add_action('template_redirect', 'require_login');

or

add_action('get_header', 'require_login');

I've always used template_redirect in the past but lately it doesn't seem to always work.

With the following function :

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

Which action should be used?

add_action('template_redirect', 'require_login');

or

add_action('get_header', 'require_login');

I've always used template_redirect in the past but lately it doesn't seem to always work.

Share Improve this question asked Jul 14, 2014 at 21:01 Bryan WillisBryan Willis 4,4793 gold badges34 silver badges47 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

When you want to protected a content, redirect non-logged users is better done as soon as possible. So, between the 2 hooks in OP 'template_redirect' is better. But you can also use an even earlier hook, to improve security and performance (by skipping unwanted queries).

'wp_loaded' for this sort of things is probably the best choice, because is pretty early but user is already set and so can be checked.

本文标签: wp adminRequire Login Redirect to Login Page