0

I have created a WordPress site and I've created a 'portal' page that is the main page that is displayed when you visit https://domain.tld. I have a plugin that forces any users that are not logged in to only be able to view this portal page. If they are logged in they are able to navigate throughout the site.

On the portal page there is a link to the home page that shows the rest of the content/navigation menu of the website. I want users that are not logged in to be able to click the link to the home page and be asked to login rather than be forced to only be able to access the portal page. I can change the link on the portal page that is pointing to the home page to point to https://domain.tld/wp-admin instead but then any user that is already logged in will be brought to their admin panel instead of the home page.

What can I do to rectify this? I am thinking about moving the WordPress installation to https://domain.tld/wp/ instead and have a static non-WordPress page on the root and force the users to login once they hit WordPress. Is there a way I can redirect logged in users away from the /wp-admin/ if they are logged in if they click the home page link?

1

Option 1 - WP Nav Menus

In your portal page template (or even in your header template) you can use wp_nav_menu() combined with is_user_logged_in() to print one of two menus.

wp_nav_menu( array(
    'theme_location' => is_user_logged_in() ? 'logged-in-menu' : 'logged-out-menu'
) );

Now you'll need to create two menus in the Wordpress admin under Appearance > Menus. One menu is your normal site menu, and the other is your special menu for logged out users which contains only a link to the login page.

You'll need to replace 'logged-in-menu' and 'logged-out-menu' with either the slug (eg: 'slug'), id (eg: 18), or the name (eg: 'Menu Name') of the two menus you created. See the documentation linked above for more info.

Option 2 - Simple Buttons

On your portal page template, you could add one of two buttons based on whether the user is logged in. Change the markup to your liking.

if ( is_user_logged_in() ) {
  // user is logged in
  ?>
  <a href="/">Enter Site</a>
  <?php
} else {
  // user is not logged in
  ?>
  <a href="/wp-admin/">Please Log In</a>
  <?php
}

Option 3 - Use a Plugin

Plugins exist that will conditionally show menu items based on user login, role, etc. One that I know of offhand is the WPFront User Role Editor. I believe the free version of that plugin will do what you want.

  • Thanks for the thorough response! I will try out option 2 to see how that works for me; seems like the simplest solution. – qroberts Jan 24 '17 at 19:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.