1

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.

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.

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.