How can I disable access non registered users? If the user is not logged in I would like to redirect them to a custom registration/login page. Is it possible this using below code as I dont want to use plugin.
<?php
if ( is_user_logged_in() ) {
echo 'Welcome, registered user!';
} else {
echo 'Welcome, visitor!';
}
?>
Thanks.
Write this into a plugin:
As plugin on GitHub.
This will force all visitors login if they arenât already.
In some cases, this is asking for a log-in every time. This might work better:
If you want to send a 404 status instead, you can replace the
auth_redirect()
with:If you don’t feel like changing your code, you could use this plugin instead: Restricted Site Access. It’s highly rated and in my personal experience, it works really well.
You probably just have to put the following in your
functions.php
:I got the Answer its easy by css..
ADD this code in header.php
Well, your code is actually functional. You just have to call the right modules in the right places.
Add This Function to your functions.php file.
when user not login access on
home page(page id ==2) to redirect on login page.
Add this code in your Child’s themes of Parent themes function.php file
Please replace “home” with the page slug name, i.e this is the page you don’t want your users to access without logging in and replace this url “https://example.com/login” with the destination url where you want to redirect your users if they are not logged in, i.e this url should direct to your login or registration page.