Show info to author only
Question
I'm writing my own plugin.
works like this:
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?
Solution
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.
OTHER TIPS
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;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange