0

So, I have a widget that I only want to show it to the page author.

I used the following logic: is_author()

However it does not seem to work (it shows non-author)

Am I using this logic right?

Any suggestions?

Thank you

1
  • Could you please post the code you've tried so far ? Commented Feb 19, 2015 at 14:19

2 Answers 2

2

is_author() is only for Archive pages. Quote from the Codex:

is_author() checks if an Author archive page is being displayed

So viewing a single Post or Page isn't going to get a TRUE from is_author().

I think you'll want something more like this:

global $post,$current_user; // get the global variables to check
get_currentuserinfo();  // get current user info
// Now check if the author of this post is the same as the current logged in user
if ($post->post_author == $current_user->ID) {
   // do code here
}

I hope this helps. :)

EDIT #1: Shortened code version.

// Check if the author of this post is the same as the current logged in user
if ( $post->post_author == get_current_user_id() ) {
   // do code here
}
6
  • 1
    +1 for the explaination of why is_author() will not work. However, can you not replace $current_user->ID with get_current_user_id()? That way you can also do away with the $current_user global and the call to get_currentuserinfo().
    – David Gard
    Commented Feb 19, 2015 at 16:01
  • 1
    In fact, if this code is in a template, as opposed to inside a function, I don't think you'd even need the $post global.
    – David Gard
    Commented Feb 19, 2015 at 16:02
  • Correct. IF it is in a template you can pair it down. It wasn't clear where this will go, although it'll be some widget area, so I'm not sure what's best for them. I will update my answer with a shortened version too. Thanks for the +1 and the suggestions.
    – Sean Grant
    Commented Feb 19, 2015 at 16:09
  • True, it's often difficult when they don't give you all of the information!
    – David Gard
    Commented Feb 19, 2015 at 16:10
  • 1
    Got it working with the following code: global $post,$current_user; get_currentuserinfo(); return ($post->post_author == $current_user->ID);
    – Steve Kim
    Commented Feb 20, 2015 at 1:39
0

You can use:

$obj = get_queried_object();
$user_id = get_current_user_id();

// check if page's post author is same as logged in user
if (isset($obj->post_author) && $obj->post_author == $user_id) {
    // ... do your stuff here
}

With get_queried_object function you get object of the current page. As it's requested that page should be of "page" type you'll get WP_Post object of the current page (post_type = 'page').

The get_current_user_id is self-explanatory.

So if both match, you'll get code running for current user which is creator of the page.

You can also place a check that queried object is 'page':

if ($obj instanceof WP_Post && $obj->post_type == 'page') {
    // ...
}
3
  • You don't need to pass the values through intval() if you are using == - that only compares values, not types.
    – David Gard
    Commented Feb 19, 2015 at 16:06
  • You can use type cast (int) also, all the same. If you want to make 100% sure.
    – mjakic
    Commented Feb 19, 2015 at 16:08
  • Thank you for the lovely reply guys. So, I am actually using a plugin called "Widget Logic" which allows me to put logics directly into the individual widget. wordpress.org/plugins/widget-logic In this case, how should I do it?
    – Steve Kim
    Commented Feb 19, 2015 at 23:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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