Danish Jamil Danish Jamil - 19 days ago 5
PHP Question

how can i restrict unregistered users from certain contents or pages in wordpress

I am developing a website with wordpress that has a comment area in photo gallery and some tutorial videos page. how can i restrict unlogged users from giving comments and going to the video page. Till yet i have Google a lot but did not get any solution related my issue. I have already searched a lot of plugins but it was no use,
For registration i am using pie register plugin my word-press theme is parabola.
I tried to make some changes in comment.php file in theme folder:

<?php
/**
* The template for displaying Comments.
*
* The area of the page that contains both current comments
* and the comment form. The actual display of comments is
* handled by a callback to tc_comment_callback()
*
* @package Customizr
* @since Customizr 1.0
*/

/*
* If the current post is protected by a password and
* the visitor has not yet entered the password we will
* return early without loading the comments.
*/
if ( have_comments() )
echo apply_filters( 'tc_comment_separator', '<hr class="featurette-divider '. current_filter() .'">' );
?>

<div id="comments" class="<?php echo apply_filters( 'tc_comments_wrapper_class' , 'comments-area' ) ?>" >
<?php
if( is_user_logged_in() )
{
comment_form( array( 'title_reply' => __( 'Leave a Comment' , 'customizr' ) ) );
if ( have_comments() )
do_action ( '__comment' );
}
?>
</div><!-- #comments .comments-area -->

Answer

Simply put a ! before is_user_logged_in().

For example:

if( !is_user_logged_in() ) {
    // Content shown for non-users
}

if( is_user_logged_in() ) {
    // Content shown for authorized users
}