0

I am struggling with this little problem. I have a user profile page and I want to change some of the content based on whether it is my own profile og someone elses. This is my code to add a shortcode which eventually will call other shortcodes.

function is_mine_func() {
if (is_author() ) {
    echo 'it is my site';
   // echo do_shortcode('[wpfepp_submission_form form="2"]' );
    }
else 
    {
        echo 'its not your site';
    //echo do_shortcode('[userpro template=postsbyuser postsbyuser_mode=grid]' );
    }
}
add_shortcode( 'minminmin', 'is_mine_func' ); 

I have a feeling that is_author is not the right way to do so since I always get the 'its not your site' message no matter if I am the author of the page or not.

Should is_author() not evaluate if I am the author of 'this' page or not?

0
2

Per the Codex, is_author() checks to see if you're on an Author Archive page (ie, a page listing all the posts/pages/etc that a given user has authored). If you're on any other type of page, is_author() will return false.

What you're looking for sounds more along these lines:

if ( in_the_loop() )
{
    get_currentuserinfo();
    print ( get_the_author() === $GLOBALS['current_user']->display_name )
        ? 'It is my site.'
        : 'It is not my site.';
}

This code must be used in The Loop.

References

2
  • This makes sense too. But it is as if it always evaluate as false. aka. It is never my site – odinp123 Oct 13 '14 at 15:00
  • Thanks you are right. We have some plugin functions that overwrite this. – odinp123 Oct 13 '14 at 15:10
2

I'd try to use author IDs rather than the username:

if ( get_current_user_id() == get_the_author_meta( 'ID' ) ) {
    echo "It is my site.";
else {
    echo "It's not my site";
};

References

2
  • 1
    I considered comparing IDs, but get_the_author() returns the author's display name, so that's what I went with. Also, the_author_meta() displays the metadata; you should probably use get_the_author_meta() instead. – Pat J Oct 13 '14 at 16:53
  • Changed answer use get_the_author_meta... silly me! – MetalAdam Oct 13 '14 at 16:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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