Hide a page if a user is not logged in

Sometimes you might want to hide a page if a user is not logged in. To do this you can use this simple snippet to call a page from within your theme folder called ‘not-logged-in.php’ but, remember this will call before the header, footer and the bits in between so the file that is called should contain the whole code for the page including the html, head and body; opening and closing tags.

<?php
/* Template Name: Only for logged in users */
if ( !is_user_logged_in()) {
get_template_part('not-logged-in');
exit(0);
}
get_header();
?>

the file that’s called might be something like this:

<!DOCTYPE HTML>
<html>
<head>
<title>Ooops! You need to login.</title>
</head>
<body>
<h1>To view this page you need to login.</h1>
</body>
</html>

source: Developing a WordPress site on a live url

About

A geek who likes to cook with wp ingredients!

Leave a Reply

Your email address will not be published. Required fields are marked *