简体   繁体   中英

Wordpress: Display “Edit Profile” link to author only

I have a WordPress website with a custom theme. I'm using custom author template page to display a public "profile" of the user's page. I want to show a link to the edit page (I have a template for that too) to ONLY the logged in user who is also that particular author.

I have the following code, but it shows the edit link to ALL logged in users. I don't want users to think they can edit other authors' profiles.

<?php global $user_id, $user_login; 
get_currentuserinfo();  
$author_id = $curauth->user_id; 

if($user_id !== '' && $author_id == $user_id){
    echo 'EDIT LINK HERE';
}

?>

You're almost there. Don't worry about the global variables, they'll just mess things up. What you want is this:

<?php
if(get_query_var('author_name')) :
    $curauth = get_user_by('slug', get_query_var('author_name'));
else :
    $curauth = get_userdata(get_query_var('author'));
endif;

get_currentuserinfo();

if( $curauth->ID == $user_ID) {

    // Do your edit link work here ...

}
?>

This first loads the current author based on the query variable used to generate the author profile page. That's how you get $curauth->ID . Then it loads up all of the standard information for the current user (see the Codex for a full list of the variables populated by get_currentuserinfo() ). Then it does a simple comparison between the two values ... no need to check that $user_ID has a value, because a null value for that variable won't be equal to $curauth->ID anyway.

FWIW I've tested this on WP 3.0.1 and you should be good to go.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM