0

I am trying to create a page template that restricts content to only logged-in users, if they are not logged in then they are re-directed to a specific page. I have created a page template called Restrict Access.

Here is the code that I came up with, I have tried many times to put this snippet in the page.php below and it still shows the content. Can someone please help me and put this in where it is supposed to go? Also could you look the snippet over and see if this is even good?

<?php
// Check If user is logged in - if so then show the content - if not then 
//redirect to purchase course page
if (!is_user_logged_in() ){
echo "Restricted Content!";
echo '<a href="http://example.com">Go to Home Page</a>';

}else{
// Show content
the_content();
}
?>

Here is my page.php code

<?php /* Template Name: Restrict Access*/
$options = thrive_get_options_for_post(get_the_ID());

$main_content_class = ($options['sidebar_alignement'] == "right" || 
$options['sidebar_alignement'] == "left") ? $options['sidebar_alignement'] : 
"";

if ($options['sidebar_alignement'] == "right") {
$main_content_class = "left";
} elseif ($options['sidebar_alignement'] == "left") {
$main_content_class = "right";
} else {
$main_content_class = "fullWidth";
}
$sidebar_is_active = _thrive_is_active_sidebar($options);

if (!$sidebar_is_active) {
$main_content_class = "fullWidth";
}

get_header();
?>
<?php if ($options['sidebar_alignement'] == "left" && $sidebar_is_active): ?
>
<?php get_sidebar(); ?>
<?php endif; ?>
<?php if ($sidebar_is_active): ?>
<div class="bSeCont">
<?php endif; ?>
<section class="bSe <?php echo $main_content_class; ?>">

    <?php if (have_posts()): ?>

        <?php while (have_posts()): ?>

            <?php the_post(); ?>

            <?php get_template_part('content', 'single'); ?>

            <?php if (comments_open() && !post_password_required() && $options['comments_on_pages'] != 0) : ?>
                <?php comments_template('', true); ?>
            <?php elseif ((!comments_open() || post_password_required()) && get_comments_number() > 0): ?>
                <?php comments_template('/comments-disabled.php'); ?>
            <?php endif; ?>

        <?php endwhile; ?>

    <?php else: ?>

    <?php endif ?>

</section>

1 Answer 1

0

This could be done in a number of ways. Please refer to the codex for further info on template hierarchy, the wordpress loop and the 'get_template_part()' function. The page.php is for dispalying every page. You should copy and rename it to page-restricted.php.

From the original page.php, remove this part - /* Template Name: Restrict Access*/. Be sure to include it in page-restricted.php. By this, you ensure that normally page contents are displayed, it's only hidden when you set the Restrict Access template in the admin when editing the page. Your code should be rewritten then thus:

<?php while (have_posts()): ?>

    <?php the_post(); ?>

    <?php if ( !is_user_logged_in() ) {

        echo "Restricted Content!";
        echo '<a href="http://example.com">Go to Home Page</a>';

    } else {

        get_template_part('content', 'single'); ?>

    } ?>

    <?php if (comments_open() && !post_password_required() && $options['comments_on_pages'] != 0) : ?>
        <?php comments_template('', true); ?>
    <?php elseif ((!comments_open() || post_password_required()) && get_comments_number() > 0): ?>
        <?php comments_template('/comments-disabled.php'); ?>
    <?php endif; ?>

<?php endwhile; ?>
2
  • Ok so I take the original page.php above and rename it to pageRestricted.php and then I copy the code you have above and place it where at in the original code from page.php?4
    – user133608
    Commented Dec 20, 2017 at 19:20
  • Yes, you make a copy of it, and rename the copy. If you modify the original, all of your pages will be hidden to non-logged-in users.
    – galingong
    Commented Dec 20, 2017 at 19:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.