Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Force Login Except Specified Template WordPress


I need a template tag to force a user to my login page unless they are on a specific custom template.

( - I am not using the standard wp-login)

So I am thinking the best way to handle this would be something like !is_user_logged_in() in my header file. Since the template file I want "Public" I don't include my main theme header anyways. But perhaps there is a better way?

What should I place in my template/header to redirect the user to the my login page unless they are..

A. Logged in
B. On a Custom template page.

Thank you!

Answers (2)


Andrzej answers:

I'd add this to functions.php file:

function my_force_login() {
$id = 45;
if ( !is_user_logged_in() && !is_page($id)) {
add_action('template_redirect', 'my_force_login');

Obv. replacing $id with ID of your custom login page.

Tell me if that works for you? (just corrected some codes to make it simpler and more stable).

Can you also tell me if you're using your custom template only on one login page, or there could be more diffrent login pages?


Pippin Williamson answers:

Add this to your header.php:

<?php if (!is_user_logged_in() ) {
wp_redirect(''); exit;

platinum comments:

I ended up using your solution but replaced get_page_link from above.

<?php if (!is_user_logged_in() ) {
wp_redirect(get_page_link(13)); exit;