I am new to wordpress. I am running wordpress setup on http://localhost/wordpress/
. I am facing two problems right now:
- Only the logged in users can access the site. So, I am trying to redirect the guest from home page to login page using the following code which is somehow isn't working:
Path:
wp-content/themes/twentysixteen/header.php
<?php
if(get_permalink() != wp_login_url() && !is_user_logged_in()){
wp_redirect( wp_login_url() ); exit;
}
?>
Since the above code wasn't working, I tried to move on by letting the user to login manually by clicking on the login
button. Here is the working code:
<?php
if(get_permalink() != wp_login_url() && !is_user_logged_in()){
// wp_redirect( wp_login_url() ); exit;
?>
<a href="<?php echo wp_login_url( get_permalink() ); ?>" title="Login">Login</a>
<?php
}
?>
- From above code, when guest clicked on login he/she was redirecting to login page and When the user is logs in, the page is redirecting to wordpress admin i.e
wp-admin
instead of home page i.ehttp://localhost/wordpress
.
What I am trying to do is:
- Redirect the guest from home page to login page, if the user isn't logged in.
- And then redirect the user from login page to home page instead of wp-admin, when user logs in.
http://localhost/wordpress
.http://localhost/wordpress
, they must be redirected to login page. and when the log in, they must be redirected tohttp::localhost/wordpress
. Apart from the above description, if the guest is on other page than the home page for examplehttp://localhost/wordpress/about
then the guest will still be redirected to login page and when he/she logs in, they will be redirected tohttp://localhost/wordpress/about
.