• I have a page that has to be protected behind a login — not just password-protected (which is in “Pages”), but an actual user login. How does one make this work in a WordPress menu, so that if someone clicks on the link they’re prompted to log in, but if they’re already logged in they go straight to the page?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Your theme would have to support this; or at least provide customized hooks that you can use to ‘hook’ into the code at that point.

    You could create a page template for the page you want to protect, drop it into a child theme folder, and add your logic there. When you go to edit the page in the editor, select this new page template.

    In the template, you will basically say something like:
    1. if user is logged in… continue.
    2. else display login form.

    Thread Starter Who_Dat

    (@who_dat)

    Josh, thanks for the reply. Actually, I figured out a different approach: rather than try to put the conditional logic in the menu, I used it to call one of two alternate menus, one for those logged in and one for those not logged in.

    <?php
    if ( is_user_logged_in() ) {
    wp_nav_menu( array( 'theme_location' => 'thisNav' ));
    } else {
    wp_nav_menu( array( 'theme_location' => 'thatNav' ));
    }
    ?>

    That’s in header.php. So far, it seems to work fine.

    It’s highly discouraged making changes to core WordPress/theme/plugin files. Any changes you make will be overwritten when you update the product.

    Instead, consider creating a child theme. Then, add your theme ‘header.php’ file into your child theme directory; and make any modifications there. This will ensure they are preserved anytime you update your theme.

    Thread Starter Who_Dat

    (@who_dat)

    That’s okay, it’s my own theme.

    Perfect.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘How do I add conditional logic to a WordPress menu?’ is closed to new replies.