Show info to author only

I’m writing my own plugin.

works like this:

Read More
function myfunc (){
    $a = 'hello';
    // the sentence what Am looking for: 
    if ( user is logged and is the author of current post / page ){
        $a= 'author';
    }
    return $a; 
}

Wondering how I can do that sencence / function?

Related posts

Leave a Reply

2 comments

  1. Just compare display names:

    $currentuser = get_currentuserinfo();
    
    if ( get_the_author() == $currentuser->displayname ) {
         // current user is the post author; do something
    }
    

    The get_the_author() function returns displayname, which should be able to be compared against the displayname parameter that is a part of the $currentuser object.

  2. You can get this information using the current_user and post variables.

    function myfunc (){
        global $current_user, $post;
        $a = 'hello';
        // check if current user id matches post author id
        if ( $current_user->ID == $post->post_author ){
            $a = 'author';
        }
        return $a; 
    }