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>