Skip to:
Content
Pages
Categories
Search
Top
Bottom

[RESOLVED]Hide pages from non-members

  • I want to know how to hide pages from anyone who visits my site that is not a member/logged in. So far if I go onto my site as a ‘non-member’ I can still see the pages ‘activity’ ‘groups’ ‘members’ etc. I would like to know how to make these invisible to anyone not logged on. But obviously visible the second you sign in. By invisible I don’t mean a redirect to homepage/login if someone clicks on the page. But so as you don’t even see the option on the homepage.

Viewing 13 replies - 1 through 13 (of 13 total)

  • aces
    Participant

    @aces

    One way of doing that is by serving different menus depending whether someone is logged in or not such as
    `
    <?php
    // https://codex.wordpress.org/Function_Reference/wp_get_current_user
    $current_user = wp_get_current_user();
    if ( 0 == $current_user->ID ) {
    wp_nav_menu( array( ‘container’ => false, ‘menu_id’ => ‘nav’, ‘theme_location’ => ‘secondary-menu’, ‘fallback_cb’ => ” ) );
    // Not logged in.
    } else {
    wp_nav_menu( array( ‘container’ => false, ‘menu_id’ => ‘nav’, ‘theme_location’ => ‘primary’, ‘fallback_cb’ => ” ) );
    // Logged in.
    }
    ?>
    `
    or another approach:
    `
    <?php
    if ( is_user_logged_in() ) {
    wp_nav_menu( array( ‘container’ => false, ‘menu_id’ => ‘nav’, ‘theme_location’ => ‘primary’, ‘fallback_cb’ => ” ) );
    } else {
    wp_nav_menu( array( ‘container’ => false, ‘menu_id’ => ‘nav’, ‘theme_location’ => ‘secondary-menu’, ‘fallback_cb’ => ” ) );
    }
    ?>
    `
    replacing the `wp_nav_menu` bit in the ( child ) theme’s header.php file. It may need adapting depending on theme etc. Remember to make backups before editing!

    Then use the walled garden or similar techniques to redirect users if they try to reach the page directly…

    I prefer the first option of serving different menus whether the user is logged in or not. Should I put that code into the themes header or funtion.php? Do I need to add to the given code it to specify which pages I want to hide from logged out users. Using latest version of wordpress and buddypress

    I am guessing that I need to make a change in the wordpress-includes pluggable.php


    aces
    Participant

    @aces

    You shouldn’t touch wp or bp core files.

    What theme are you using?

    In your child theme’s header.php file you replace the `wp_nav_menu` section with either of the above ( not both ).

    Then you can define two menus on appearance > menus ( primary and secondary-menu on the above example ) Set one for logged in users and one for everyone else.

    Just buddypress original for now. So in the ‘menu_ID’ do I just replace that with a ‘(then my page ID’ then im guessing a comma, then ‘(page Id)’ right?

    for example wp_nav_menu( array( ‘container’ => false, ‘100’ => ‘nav’, ‘theme_location’ => ‘primary’, ‘fallback_cb’ => ” ) );
    } else {

    Which should result in a nonmember/non logged not being able to see my activities page.


    aces
    Participant

    @aces

    You should make a child of bp-default and backup files and db just in case…

    You don’t add or remove any pages from the code. The point is that it works from the built in wordpress default menu system. ( https://codex.wordpress.org/Navigation_Menus )

    To get the second menu to appear – In your child theme’s functions.php file add:
    `function register_my_menus() {
    register_nav_menus( array(
    ‘primary’ => __( ‘Header Navigation’ ),
    ‘secondary-menu’ => __( ‘Alternative Menu’ ),
    ‘another-menu’ => __( ‘Another Menu’ )
    ) );
    }
    add_action( ‘init’, ‘register_my_menus’ );
    `

    OK alternative menu etc is all set up on menus page. I am using the Elbee Elgee child theme at the moment. Do I still have to do the first step of implementing that code in the headers menu (<?php
    // https://codex.wordpress.org/Function_Reference/wp_get_current_user
    $current_user = wp_get_current_user();……


    aces
    Participant

    @aces

    Yes!

    Which menu is served depends on whether they are logged in or not and that is the point to decide…

    and please use backticks ` ` ` when quoting code

    It’s frustrating because that’s almost it. I leave the Header navigation as blank because my buddypress/wordpress pages automatically fill in that menu. So when I am logged in all the pages I want to see are there. I only made my homepage and about page available to the nonmembers, and that works fine too. However, when a user is logged off, they can still see all the pages available to a logged in user aswell as the pages which are available to them. So instead of having one smooth menu for the non member, theres the members menu and below it a non members menu. Note this is only for logged out members everything is fine as far as signed in users are concerned.


    aces
    Participant

    @aces

    You need to create both menus. Without it wordpress has it’s own default menu which you have been relying on

    A good guide for understanding wordpress menus is http://justintadlock.com/archives/2010/06/01/goodbye-headaches-hello-menus

    Yes, it finally works. Thank you so much for that! I do have more questions, but i’ll leave that for another day. Feel guilty constantly asking. But yea good advice, thanks again.

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘[RESOLVED]Hide pages from non-members’ is closed to new replies.
Skip to toolbar