Home » Restrict Pages Or Posts Access Without Login Into WordPress

Restrict Pages Or Posts Access Without Login Into WordPress

Last updated on June 16, 2022 by

Are you looking for a way to restrict pages or posts access without Login into WordPress? If yes, then you are at the right place. In this article, we will see how WordPress restrict page or post access to logged in users by taking a basic example for better understanding.

Usually, installing lots of plugins on the site can impact performance and security. We are recommending you avoid installing plugins for smaller tasks like this. Are you curious to see? Let’s just jump right into it.

Table of Contents
1. An Ideal Place to Add Custom Code in WordPress
2. What Are We Doing In Very Simple Word?
3. Restrict All Pages for Not Logged In Users Except Login Page In WordPress
4. Restrict Specific Pages for Not Logged In Users In WordPress

Now, to accomplish the WordPress restrict page access to logged in users, we will see some extra stuff like where to add code in WordPress & What will do by taking some examples. Let’s see how to do it.

An Ideal Place to Add Custom Code in WordPress

Most Important: Add the following code to your child theme’s functions.php file. If you add custom code directly to your parent theme’s functions.php file then it will be wiped entirely when you update the theme.

If you are using a custom theme and if it doesn’t require any update then you can directly place the code into wp-content/themes/your-theme/function.php file.

Please note that we have tested all the below codes with Hello Elementor’s child theme.

What Are We Doing In Very Simple Word?

Here, We will use the template_redirect hook. This hook fires before determining which template to load. So with the help of this hook, We will check whether the user is Logged In or Not. If the user does not Logged In then restrict them to posts or pages.

Does it make sense to you? It sounds very very simple. Isn’t it? Let’s go ahead and do that.

01 Restrict All Pages For Not Logged In Users Except Login Page In WordPress

In this step, We will restrict all page access to logged in users in WordPress. For that, we have used a get_queried_object_id() function that will give us the current page or post ID which we used to check whether the page is a login page or not. If we don’t add this check then WordPress will also prevent you to see the Login page.

Then, we have added the condition if ( !is_user_logged_in() && $page_id !== $login_page_id ) : which will check if a user is logged in or not and the current page is not the login page.

If the user is not logged in and the current page is not a login page then it will show you the “You are not allowed to access this page” message.

Let’s add the below code to your function.php file and see the result:

if( !function_exists('tf_restrict_access_without_login') ):

	add_action( 'template_redirect', 'tf_restrict_access_without_login' );

	function tf_restrict_access_without_login(){
		
		/* get current page or post ID */
		$page_id = get_queried_object_id();

                /* replace your login page ID here */
		$login_page_id = 183;

		if ( !is_user_logged_in() && $page_id !== $login_page_id ) :

			wp_die('You are not allowed to access this page');
			return;
			exit;
		endif;
	}
endif;

In order to make the above example works, You need to set your login page ID so replace 183 ID with your login page ID in $login_page_id a variable.

Now, Try to access any WordPress page except login then it will show something like the below.

WordPress Login Restriction Message
Figure 1: WordPress Login Restriction Message

Then, Let’s try to access the Login page. We can access the login page as follows and after the login, WordPress will allow you to access all the pages or posts.

WordPress Login Demo for Access Restriction
Figure 2: WordPress Login Page Demo

02 Restrict Specific Pages For Non Logged In Users In WordPress

In this step, We will restrict specific page access to logged in users in WordPress. For that, we have used a get_queried_object_id() function that will give us the current page or post ID which we used to check whether the page is a login page or not. If we don’t add this check then WordPress will also prevent you to see the Login page.

After that, we added an array of page IDs that we want to restrict $behind_login_pages = [ 2, 5, 141 ];, you need to add your pages or post IDs to it.

Then, we have added the condition if( ( !empty($behind_login_pages) && in_array($page_id, $behind_login_pages) ) && !is_user_logged_in() ): which will check if a user is logged in or not and whether the current page is restricted or not.

If the user is not logged in and the current page is restricted then it will show you the “You are not allowed to access this page” message.

Let’s add the below code to your function.php file and see the result:

if( !function_exists('tf_restrict_access_without_login') ):

	add_action( 'template_redirect', 'tf_restrict_access_without_login' );

	function tf_restrict_access_without_login(){
		
		/* get current page or post ID */
		$page_id = get_queried_object_id();

		/* add lists of page or post IDs for restriction */
		$behind_login_pages = [ 2, 5, 141 ];

		if( ( !empty($behind_login_pages) && in_array($page_id, $behind_login_pages) ) && !is_user_logged_in() ):

			wp_die('You are not allowed to access this page');
			return;
			exit;

		endif;
	}

endif;

In order to make the above example works, You need to set your pages or posts IDs in $behind_login_pages variable.

Now, Try to access the restricted pages or posts, you will experience the “You are not allowed to access this page” message like Figure 1 as above.

Hurray! we have just completed the minimum steps for WordPress to restrict page access to logged in users.

Additionally, read our guide:

  1. How To Add Back Button In Elementor
  2. 403 Error When Updating In Elementor
  3. How To Add Multiple Post Types In Posts Widget In Elementor
  4. How to Add Products Per Page Dropdown in WooCommerce
  5. “Sorry, your session has expired. Return to homepage” – WordPress WooCommerce Error
  6. How to Create a Plugin in WordPress from Scratch
  7. How to Disable Admin Bar in WordPress Without Plugin
  8. How To Send Custom Emails in WordPress
  9. How to Allow Preview of Draft Post Without Login in WordPress
  10. Import Users From CSV In WordPress Programmatically
  11. Dynamically Populate A Select Field’s Choices In ACF
  12. How To Create Database Table In WordPress
  13. Difference Of require, include, require_once And include_once
  14. PHP: Get Day Name From Date
  15. PHP: Get Year From Date
  16. How To Update Pivot Table In Laravel
  17. Laravel 9 Resource Controller And Route With Example
  18. Laravel 9 File Upload Tutorial With Example
  19. How To Schedule Tasks In Laravel With Example
  20. Laravel Collection Push() And Put() With Example

That’s all. We hope this helped you to learn How to Restrict Pages or Post Access Without Login to WordPress.

Please let us know in the comments if everything worked as expected, your issues, or any questions. If you think this article saved your time & money, please do comment, share, like & subscribe. Thank you in advance 🙂. Keep Smiling! Happy Coding!

 
 

7 thoughts on “Restrict Pages Or Posts Access Without Login Into WordPress”

  1. The training was useful
    After reading this post, I checked all the WordPress posts
    *****************

    I need an option or code for each user in each role to see only their posts at the front end, and other users’ posts to be completely hidden from them, even in search results (without any messages or effects).

    More details:

    The user has to log in to receive the content, and when they log in, they only sees their posts (as if there are no other posts on the website).

    I need all pages to be excluded (show for all). Only hide other users’ posts

    Example image
    https://ibb.co/Y7Bc9JZ

    I want every user to see only their posts when logged in.
    Visible items for all users:
    1- Pages
    2- category
    3- tags
    Or anything in common between user posts.

    Unsigned users do not see any content, not even pages
    ***********
    Any help would be appreciated

    Your website is beautiful

    Reply
  2. Hi,

    Thanks for the article, it’s great to be able to make changes to a site without adding a new plugin every time!

    Can I ask, is it possible to redirect the guest to a specific page rather than just show them the wp dead screen?

    Reply

Leave a Comment