🚀 Is building a website too difficult? Let me guide you step by step—Learn about the 「Naibabiji WordPress Website Building Coaching Service」 →

Method to make a WordPress website accessible only to registered and logged-in members

building your own websiteThere are often two situations where you don't want others to view the website content. 1. A purely personal blog that you don't want others to see. 2. Certain internal websites that you don't want irrelevant people to access. 3. Special resource sites. So, if your website is built withWordPressyou can easily achieveWordPress PluginWordPress login required to viewfunctionality throughBelow are a few plugins shared by Naiba to achieveWordPress requires registration to view.

Registered Users Only

The Registered Users Only plugin was seen on WP Jianzhan Ba. Its function is very simple: after installation and activation, all unregistered and non-logged-in users will be redirected to the login page.Registered Users OnlyIf you want to allow open registration, then check the option to allow anyone to register. If you want visitors to temporarily access the website content, then check the guest mode.Download Link

Force Login

Force Login is simpler; after downloading and activating, it directly enablesThe entire site requires login to viewfunction. There is no separate settings page; enabling the plugin requires login to view, and disabling the plugin does not require login to view website content.Download Link

My Private Site

My Private Site, this plugin is more suitable for resource marketing websites because, in addition to redirecting unlogged users to the login page, it can also customize the login interface and registration interface. Furthermore, the most powerful feature of My Private Site is the ability to exclude pages. You can set the homepage to be viewable without login and add other pages that do not require login to view. At the same time, if login is required to view pages, it also supports redirecting to the page before login after logging in.Introduction to the simple usage of My Private Site:Check Private Site to enable login access. Custom Login is the option to customize the login page. Visible Exclusions is the option to exclude pages.Download Link

Show/Hide as Needed

If you want to set methods to display specific content and hide specific content yourself, you can useWicked Block ConditionsIf using code, here is the following
add_shortcode('hide','loginvisible');
function loginvisible($atts,$content=null){
if(is_user_logged_in() && !is_null($content) && !is_feed())
return $content;
return '';
}
Then, in the article, wrap the hidden content with [hide] and [\/hide]. If you just want to simply hide articles from the website homepage or category pages, you can refer to:Method to Hide Specific Posts on the WordPress Website Homepage or Category Page

Visible after entering password

If you want to implement that WordPress posts, categories, or the entire website require a password to view or access, you can refer to this article:3 WordPress Plugins That Require a Password to Access the Website_Password to View Content

Using Code to Hide Categories, Login Required to View

//template_redirect动作钩子是一定会执行的,所以用这个钩子对全站有效
add_action( 'template_redirect', 'ashuwp_show_only_login', 0 );
function ashuwp_show_only_login(){
//判断登录,about页面就允许访问
if( !is_page('about') && !is_user_logged_in() ){
auth_redirect(); //跳转到登录页面
exit();
}
}
If a category directory requires login to be visible, use the following code.
// 首页和指定分类文章可以访问
add_action( 'template_redirect', 'ashuwp_show_only_login', 0 );
function ashuwp_show_only_login(){
//判断登录,只允许访问ID为3和2的分类文章
if( !in_category( array( '3','2' ) ) && !is_home() && !is_user_logged_in() ){
auth_redirect(); //跳转到登录页面
exit();
}
}
The exclamation mark before !in_category means 'not,' meaning if it is not categories with IDs 3 and 2, and not the homepage, and not logged in, then redirect to the login page. The code takes effect after being added to the theme functions template functions.php. Note that in_category only supports first-level directories; if there are subdirectories, they all need to be listed, or add the following judgment.
if ( ! function_exists( 'post_is_in_descendant_category' ) ) {
function post_is_in_descendant_category( $cats, $_post = null ) {
foreach ( (array) $cats as $cat ) {
// get_term_children() accepts integer ID only
$descendants = get_term_children( (int) $cat, 'category' );
if ( $descendants && in_category( $descendants, $_post ) )
return true;
}
return false;
}
}
Then call it like this
<?php if ( in_category( 'fruit' ) || post_is_in_descendant_category( 'fruit' ) ) {
// These are all fruits…
}
?>
Reference

Ultimate Plugin

Not satisfied with testing the above methods one by one? Let me recommend a solution that Naiba is currently using, a powerful conditional plugin. [vk-content]This plugin, named Restrict User Access – Membership Plugin with Force, is perfect for friends who need multiple conditional checks but don't know how to rewrite code themselves.You can add multiple conditional rules, then set up user groups in Members, configure permissions in Capabilities, and save to make it effective.Download Link  [/vk-content]

🚀 Still feeling confused after reading the tutorial? Let me guide you step-by-step.

「Naibabiji WordPress Website Building Coaching Service」—From choosing a domain and buying hosting, to installing a Theme and publishing content, I「ll coach you through every step, helping you avoid detours and reach your goal directly.

👉 Learn about the Website Building Coaching Service
🔒

Comments are closed

The comment function for this article is closed. If you have any questions, please feel free to contact us through other channels.

×
二维码

Scan QR Code to Follow

AI Website Building Assistant

🤖
Hello! I am the Naibabiji AI Assistant. How can I help you?
Quick Consultation: