Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have this wordpress site that I am making where a user has to login to see their specific gallery. My problem is that all our users are members and all the members can see all the other member pages. It's kind of hard to explain so I'll link you guys to what I have.

Once this client is logged in and you hover over "Members" > "Members Page" you can see the other clients galleries clear, even though they both need passwords. How do I hide all other pages? I thought about making page titles the same as the client name but that might be too much of an inconvenience. Any suggestions or help on this?

  • I thought that comparing userID with galleryID would work, but only in a perfect world. 1 mishap and the whole comparison would be off.

  • I then thought comparing title with username but the odds of a misspelling are too great and I have a feel would cause much frustration.

share|improve this question

2 Answers 2

up vote 5 down vote accepted
+100

Big Question.

So, first, to keep the galleries separate, it would be good to give them their own custom post type. We'll be checking for this later...

<?php
add_action( 'init', 'wpse32840_register_post_type' );
function wpse32840_register_post_type()
{
    register_post_type(
        'client_gallery',
        array(
            'label'                 => __( 'Client Galleries' ), // probably needs better lables
            'public'                => true, // allow people to see this on the front end
            'exclude_from_search'   => true, // cant be brought up via a search
            'show_in_nav_menus' => false, // not in nav menus (we'll do this manually)
            'rewrite'               => array( 'slug' => 'gallery' )
        )
    );      
}

To create gallery for each user, hook into user_register and insert a new client_gallery post. Then set a usermeta value with the post ID. We can use this later for a lot of stuff. It's also more reliable than checking for the author or the post slug, etc.

<?php
add_action( 'user_register', 'wpse32840_capture_register' );
function wpse32840_capture_register( $user_id )
{
    $userobj = get_user_by( 'id', $user_id );

    $gallery_id = wp_insert_post(
        array(
            'post_type'     => 'client_gallery',
            'post_title'    => sprintf( '%s gallery', $userobj->user_login )
        ), 
        true
    );

    if( is_wp_error( $gallery_id ) ) $gallery_id = 0;

    update_user_meta( $user_id, 'wpse32840_gallery_id', absint( $gallery_id ) );
}

You should also be able to change the gallery from the admin area, so hook into edit_user_profile to add a dropdown to change the user's gallery. Also hook into edit_user_profile_update to save the new ID if it's updated.

<?php
add_action( 'edit_user_profile', 'wpse32840_user_profile' );
function wpse32840_user_profile( $user )
{
    $gallery_id = get_user_meta( $user->ID, 'wpse32840_gallery_id', true );

    // get all of our galleries!
    $galleries = get_posts(
        array(
            'post_type'     => 'client_gallery',
            'numberposts'   => -1,
            'post_status'   => 'any'
        )
    );

    // no galleries?  bail.
    if( ! $galleries ) return;

    // nonce
    wp_nonce_field( 'wpse32840_nonce', 'wpse32840_nonce', false );

    // our fields
    echo '<h3>' . __( 'Gallery' ) . '</h3>';
    echo '<select name="wpse32840_gallery">';
    echo '<option ' . selected( $gallery_id, 0, false ). ' value="0">No gallery</option>';
    foreach( $galleries as $g )
    {
        echo '<option ' . selected( $gallery_id, $g->ID, false ) . ' value="' . absint( $g->ID ) . '">' . esc_html( $g->post_title ) . '</option>';
    }
    echo '</select>';
}

/**
 * Save the user's gallery ID from the user edit page.
 */
add_action( 'edit_user_profile_update', 'wpse32840_save_user' );
function wpse32840_save_user( $user_id )
{
    if( ! isset( $_POST['wpse32840_nonce'] ) || ! wp_verify_nonce( $_POST['wpse32840_nonce'], 'wpse32840_nonce' ) ) 
        return;

    if( ! isset( $_POST['wpse32840_gallery' ] ) )
        return;

    update_user_meta( $user_id, 'wpse32840_gallery_id', absint( $_POST['wpse32840_gallery'] ) );
}

Next up, hook into template_redirect and stop normal users from visiting these client_gallery pages. This is also where you can get the current user and their corresponding gallery. If the post you're viewing doesn't match up with their gallery, throw them back to the home page.

<?php
add_action( 'template_redirect', 'wpse32840_check_user' );
function wpse32840_check_user()
{
    // not on a client gallery?  bail
    if( ! is_singular( 'client_gallery' ) ) return;

    // is the is an admin?
    if( current_user_can( 'manage_options' ) ) return;

    // thrown non logged in users back to the home page
    if( ! is_user_logged_in() )
    {
        wp_redirect( home_url(), 302 );
        exit();
    }

    $user = wp_get_current_user();
    $gallery_id = get_queried_object_id();

    // if the user isn't an admin or isn't assigned this gallery, send them back to the home page
    if( $gallery_id != get_user_meta( $user->ID, 'wpse32840_gallery_id', true ) )
    {
        wp_redirect( home_url(), 302 );
        exit();
    }
    // By here, the user is authenticated, let them continue on their merry way.
}

And, finally, what you really wanted to know!

Lastly, you can hook into wp_nav_menu_items to change the output of a given menu. This is a very simple example, only returning a home link and the gallery link if the user is logged in. You can build your own menu for logged in users here or keep it simple.

<?php
add_filter( 'wp_nav_menu_items', 'wpse32840_filter_nav', 10, 2 );
function wpse32840_filter_nav( $items, $args )
{
    /**
     * you can check for theme location here via $args... check to see if this is the main nav
     * in twenty eleven, we can check to see if this is the "primary" menu
     * Other thigns will be different.  If we're not in the primary menu, bail
     */
    if( 'primary' != $args->theme_location ) return $menu;

    // Not logged in?  return the menu
    if( ! is_user_logged_in() ) return $items;

    // if this is an admin, return the menu unaltered.
    if( current_user_can( 'manage_options' ) ) return $items;

    // our user is logged in an not an admin, build them a new menu

    // get our current user
    $user = wp_get_current_user();

    // get the users gallery
    $gallery_id = get_user_meta( $user->ID, 'wpse32840_gallery_id', true );
    $gallery = get_post( absint( $gallery_id ) );

    $items = '<li class="menu-item"><a href="' . esc_url( home_url() ) . '">Home</a></li>';
    $items .= '<li class="menu-item"><a href="' . esc_url( get_permalink( $gallery ) ) . '">Gallery</a></li>';
    return $items;
}

Here's that whole mess as a plugin.

share|improve this answer
    
Thanks for your help, we are running plugins for both gallery and for the private pages. I tried using the plugin you wrote and I'm not quite sure how it works. Each gallery is a page and I'm not quite sure how to give each a separate ID to match with your plugin. I'm also not quite sure what you mean by "hook". Thanks! –  Howdy_McGee Nov 7 '11 at 4:15
    
there's no option page. You'll see a new post type called "galleries". When you add a user, it will add a post/page associated with that user. Then when a client logs in they will see a menu with only the "home" link and a link to their gallery. –  chrisguitarguy Nov 7 '11 at 4:26
    
Ok, I got the client1 with gallery1 and client2 with gallery2, (1) is there any way to ask what gallery to put them to when making a user? (2) The users gallery is not showing up on the main menu and the permalink is an "archives/gallery/client2". –  Howdy_McGee Nov 7 '11 at 5:45
    
Replace your page galleries with new custom post type galleries. Then go edit the users in the Admin area. You'll see a little drop down at the bottom to change the user's gallery. –  chrisguitarguy Nov 7 '11 at 14:08
1  
It's OK I think it was one of my previous plugins that were messing with it. It works like a charm with a new install of wordpress and a custom menu. The only thing I did have to change was taking out the "Next" and "Previous" on the gallery posts. When client 1 is logged in they can click next to go to another post. ANYWAY Thank you for all your help! 5/5 plugin –  Howdy_McGee Nov 8 '11 at 19:25

You could simply use wp_get_current_user() and check against the author of the post or even set a post meta for the user ids that are allowed to view that specific post and check against that.

share|improve this answer
    
I can't check against the author because it will always be Admin. –  Howdy_McGee Nov 6 '11 at 3:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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