This website uses cookies to allow us to see how the site is used. If you continue to use this site, we assume that you are okay with this. If you want to use the sites without cookies, please see our privacy policy.

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 code and be nice ^_^

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.