WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Private author.php (3 posts)

  1. justbishop
    Member
    Posted 2 months ago #

    I'm working on using WP to act as a "client site" for a doggie daycare. Users register with a lot of personal info (address, phone #, etc.). I then have them adding "dog profiles" as posts with custom field inputs. One post per single dog.

    To display the user's info and their dogs' "profiles" on one screen, I've coded a custom author.php that outputs all of the custom field stuff I've added to profiles and posts (renamed to "dogs") on the author archive URL. What I need is to make the entire content of author.php only viewable if the user is logged in and viewing their OWN author archive. For example, if I'm logged in as "userA", I'd see nothing (or some sort of "you are not authorized to view this content" message) if I somehow navigated to:

    http://www.somedoggiedaycare.com/archives/author/userB

    UserB would be the only user of the "author" level able to see the content spit out by that URL.

    I'm not quite sure how to accomplish this. I'm thinking it will have something to do with conditionals, but I tried retrofitting some code meant to add an "edit profile" link to the author archive screen if it was the archive of the logged in user, and I kept getting errors due to the code getting confused with the post loop's if/else statements.

    Any help appreciated!

  2. justbishop
    Member
    Posted 2 months ago #

    So here's what I've got (with the help of a php guru friend who made sense of the code I'd stitiched together myself). It does work as far as keeping me from seeing the info on anyone else's author.php URL, however it's also giving the "Not available from logged in account" message to me when I view my own:

    <?php
    //Grab our current user
    global $current_user;
    get_currentuserinfo();
    
    //Get ID# of the author of the archive being viewed
    $curauth = get_the_author_meta( 'ID' );
    
    // Compare the two to see if they match
    if ( $curauth == $current_user->user_id ) : ?>
    
    If the ID#s of the logged in user and the author of the archive currently being viewed match, show the info here.
    
    <?php else : ?>
    <?php echo 'Not available from logged in acocunt.'; ?>
    <?php endif; ?>
  3. justbishop
    Member
    Posted 2 months ago #

    Got it! In the interest of helping someone else who may come along:

    <?php
    //Grab our current user
    global $current_user;
    get_currentuserinfo();
    
    //Get this archive's author ID#
    $curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author));
    $curauth = get_the_author_meta( 'ID' );
    
    // Get currently logged in user's ID#
    $current_user = wp_get_current_user();
    
    // Do they match?
    if ( $curauth == $current_user->ID ) : ?>
    
    If the ID#s of the logged in user and the author of the archive currently being viewed match, show the info here.
    
    <?php else : ?>
    <?php echo 'Not available from logged in acocunt.'; ?>
    <?php endif; ?>

Reply

You must log in to post.

About this Topic

Tags

No tags yet.