Last active
April 29, 2019 17:34
-
-
Save josephdickson/897d7a19e5e3e51ea275c510f53fd752 to your computer and use it in GitHub Desktop.
Restirct WordPress post or page to logged in users
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Single post and page template for restricting the title and content unless the user is logged in | |
* Sources: | |
* https://www.wpbeginner.com/wp-themes/create-custom-single-post-templates-for-specific-posts-or-sections-in-wordpress/ | |
* https://codex.wordpress.org/Conditional_Tags | |
* | |
* @link https://developer.wordpress.org/themes/basics/template-hierarchy/#single-post | |
* | |
* @package WordPress | |
* @subpackage Twenty_Nineteen | |
* @since 1.0.0 | |
* | |
* License: GPL 2 or later | |
* | |
* Template Name: Log in Required | |
* Template Post Type: post, page | |
* | |
*/ | |
get_header(); | |
?> | |
<div id="primary" class="content-area"> | |
<main id="main" class="site-main"> | |
<?php | |
if ( is_user_logged_in() ) { // check if user is logged in | |
/* * | |
* Start the Loop | |
* This example uses TwentyNineteen for the loop, if using a different theme subsitute that loop here. | |
* */ | |
while ( have_posts() ) : | |
the_post(); | |
get_template_part( 'template-parts/content/content', 'single' ); | |
if ( is_singular( 'attachment' ) ) { | |
// Parent post navigation. | |
the_post_navigation( | |
array( | |
/* translators: %s: parent post link */ | |
'prev_text' => sprintf( __( '<span class="meta-nav">Published in</span><span class="post-title">%s</span>', 'twentynineteen' ), '%title' ), | |
) | |
); | |
} elseif ( is_singular( 'post' ) ) { | |
// Previous/next post navigation. | |
the_post_navigation( | |
array( | |
'next_text' => '<span class="meta-nav" aria-hidden="true">' . __( 'Next Post', 'twentynineteen' ) . '</span> ' . | |
'<span class="screen-reader-text">' . __( 'Next post:', 'twentynineteen' ) . '</span> <br/>' . | |
'<span class="post-title">%title</span>', | |
'prev_text' => '<span class="meta-nav" aria-hidden="true">' . __( 'Previous Post', 'twentynineteen' ) . '</span> ' . | |
'<span class="screen-reader-text">' . __( 'Previous post:', 'twentynineteen' ) . '</span> <br/>' . | |
'<span class="post-title">%title</span>', | |
) | |
); | |
} | |
// If comments are open or we have at least one comment, load up the comment template. | |
if ( comments_open() || get_comments_number() ) { | |
comments_template(); | |
} | |
endwhile; // End of the TwentyNineteen loop. | |
} else { // If not logged in present a link to log into the site | |
echo '<p style="text-align: center;"><a href="' . wp_login_url( get_permalink() ) . '" title="Login">Log in to view this post</a></p>'; | |
} | |
?> | |
</main><!-- #main --> | |
</div><!-- #primary --> | |
<?php | |
get_footer(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Single post and page template for restricting the title and content unless the user is logged in | |
* Sources: | |
* https://www.wpbeginner.com/wp-themes/create-custom-single-post-templates-for-specific-posts-or-sections-in-wordpress/ | |
* https://codex.wordpress.org/Conditional_Tags | |
* | |
* @link https://developer.wordpress.org/themes/basics/template-hierarchy/#single-post | |
* | |
* License: GPL 2 or later | |
* | |
* Template Name: Log in Required | |
* Template Post Type: post, page | |
* | |
*/ | |
get_header(); | |
?> | |
<div id="primary" class="content-area"> | |
<main id="main" class="site-main"> | |
<?php | |
if ( is_user_logged_in() ) { // check if user is logged in | |
// You will want to edit the following loop to match your child theme design for single.php ando page.php etc. | |
while ( have_posts() ) : | |
the_post(); | |
the_title(); | |
the_content(); | |
// If comments are open or we have at least one comment, load up the comment template. | |
if ( comments_open() || get_comments_number() ) : | |
comments_template(); | |
endif; | |
endwhile; // End of the loop. | |
} else { // If not logged in present a link to log into the site | |
echo '<p style="text-align: center;"><a href="' . wp_login_url( get_permalink() ) . '" title="Login">Log in to view this post</a></p>'; | |
} | |
?> | |
</main><!-- #main --> | |
</div><!-- #primary --> | |
<?php | |
get_footer(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment