WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Image display based on username? (25 posts)

  1. Kahil
    Member
    Posted 7 years ago #

    Heya,

    Ok, so I am also using bbpress. There I have a plugin that lets people upload an avatar. When the image gets uploaded, it gets renamed to their username in all lowercase. I want to display that image in my wordpress sidebar but for some reason can't get it to do so.

    Here is what I have...

    <img src="/forum/avatars/<?php echo $strtolower($user->user_identity); ?>.jpg" alt="avatar" />

    Can anyone help?

  2. cpoteet
    Member
    Posted 7 years ago #

    First, you might want to put all bbPress questions in their forum. What is returned by that call?

  3. Kahil
    Member
    Posted 7 years ago #

    I have tried over there, but there aren't many users over there to help. I figured that because I am trying to do this in WordPress, that this would be the best place to ask.

    it returns a full image url except for the file name.

    .../forum/avatars/.jpg is what gets out putted...

    The plugin uploads the image file to the avatars folder and renames it based on the username and changes it so that it is all lowercase, which the users are shared out between the wordpress and bbpress databases. All Being that the wordpress and bbpress usernames are the same, there shouldn't be any issues there. In theory, I should just be able to what I had above right?

    Thanks for the help!

  4. cpoteet
    Member
    Posted 7 years ago #

    <?php get_user_name( $user->ID ); ?>

  5. Kahil
    Member
    Posted 7 years ago #

    That didn't work. It caused a fatal error... :(

  6. cpoteet
    Member
    Posted 7 years ago #

    Are you trying to get the name while in a loop?

  7. Kahil
    Member
    Posted 7 years ago #

    no, in my sidebar... I didn't know it hat to be. Ok, so now I know...is it possible to get it in the sidebar like that? So it uses the strtolower function to make it all lowercase?

    Thank you

  8. Samuel Wood (Otto)
    Tech Ninja
    Posted 7 years ago #

    Try this:
    <img src="/forum/avatars/<?php echo strtolower($user->user_identity); ?>.jpg" alt="avatar" />

    No $ in front of strtolower.

  9. Kahil
    Member
    Posted 7 years ago #

    it still isn't doing so... :(

    Thanks for all the help guys

  10. Kahil
    Member
    Posted 7 years ago #

    maybe it does need the loop... does anyone know how that would work? I've tried using the look in my sidebar before but gave up on that because I never could get it to work correctly.

    Thank you

  11. Samuel Wood (Otto)
    Tech Ninja
    Posted 7 years ago #

    You don't need to be in the Loop.

    Try using $user->display_name instead of "user_identity".

    And this will be the picture of the logged in user, of course.. What, exactly, are you trying to do?

  12. Kahil
    Member
    Posted 7 years ago #

    That didn't work neither....

    basically i have wordpress and bbpress using the same database, so all the tables are in the same place. I have a bbpress plugin called "avatar upload". It lets users upload an avatar for their posts in the forum. This plugin will upload an image to a specific directory, in this case it is yourkahil.com/forum/avatars/. When it uploads the image, it renames the image to the user's username in all lowercase. In theory, I should be able to do the same within wordpress as far as displaying the image. Being that both wordpress and bbpress are using the same usernames... I have a section of my sidebar set aside for user information. In there I would like to display a users avatar if they are logged in. Eventually I would like to make it so that if a visitor isn't logged in, it will just either display nothing or display a default image.

    Thank you

  13. Samuel Wood (Otto)
    Tech Ninja
    Posted 7 years ago #

    What does it actually output? I'm looking at http://yourkahil.com/ and see the img code in the page source, but since I'm not logged in, I clearly don't see any image (no $user = no identity for me to have).

    What is in the source that *you* see when you are logged in?

    Maybe you should add this to the code:
    <?php $user = wp_get_current_user(); ?>
    That will make sure that the $user variable is set correctly.

  14. Kahil
    Member
    Posted 7 years ago #

    that didn't work neither... :(

    it just says:

    yourkahil.com/forum/avatars/.jpg

  15. Kahil
    Member
    Posted 7 years ago #

    I just tried it from within the loop and none of the above worked...

  16. whooami
    Member
    Posted 7 years ago #

    ugh.

    http://www.village-idiot.org/broke/kahil/wine.php

    THIS works.

    <img src="<?php echo strtolower($userdata->user_login); ?>.gif" alt="avatar" />

    ALL that is in that file is this:

    <?php
    /* Don't remove this line. */
    require('../../wp-blog-header.php');
    
    echo $userdata->user_login;
    ?>
    <br/>
    <img src="<?php echo strtolower($userdata->user_login); ?>.gif" alt="avatar" />
  17. Kahil
    Member
    Posted 7 years ago #

    i'm sorry whooami, but that doesn't work. I clicked the link you posted and there was nothing there. I inserted exactly what you posted and it didn't work.

    Thank you for helping

  18. whooami
    Member
    Posted 7 years ago #

    Kahil,

    The image displayed is YOURS. thats why YOU see nothing, and thats why I pasted the PHP source.

    And it does work, I assure you. If you have trouble with it -- then thats an issue you are having. the code is correct and works exactly as it should.

    http://www.village-idiot.org/broke/commonsense.gif

  19. Kahil
    Member
    Posted 7 years ago #

    I posted exactly what you did and nothing but the alt text gets displayed. and yes i edited to reflect my site. in the source, all that displays is this:

    <img src="http://yourkahil.com/forum/avatars/.jpg" alt="avatar">

    here is exactly what i put in my sidebar...

    <?php
    /* Don't remove this line. */
    require('http://yourkahil.com/wp-blog-header.php');

    echo $userdata->user_login;
    ?>
    <img src="http://yourkahil.com/forum/avatars/<?php echo strtolower($userdata->user_login); ?>.jpg" alt="avatar" />

  20. whooami
    Member
    Posted 7 years ago #

    why are you including wp-blog-header.php in your sidebar?

    I merely included it because I'm using a separate page to test that it does, in fact,work.

    I'm not responding beyond what I've already provided - as Ive stated, the code works as expected. I've even tested this on 2.2 install.

    G'luck.

  21. Kahil
    Member
    Posted 7 years ago #

    I included it because you said to. I assumed you read what I posted in my previous posts here where I state that I am attempting to do so in my sidebar.

    again. The bbpress plugin I am using called "Avatar Upload" does the following. It will upload an image to the .../forum/avatars/ directory. While doing so, it renames the image to the user's username but in all lowercase. I simply would like to call that image based on the user. So if I log in, in my sidebar it will display my avatar. If you log in it will display yours. etc.

  22. whooami
    Member
    Posted 7 years ago #

    I included it because you said to.

    No. I did not.

    I said, quite clearly:

    THIS works.

    <img src="<?php echo strtolower($userdata->user_login); ?>.gif" alt="avatar" />

    I knew I should have left this thread alone.

    Moving on.

  23. Kahil
    Member
    Posted 7 years ago #

    Whew... thats a relief.

    Does anyone know of a plugin for wordpress that uses images like this in any way? Where it calls an image based on the logged in user viewing the page?

    Thank you

  24. Kahil
    Member
    Posted 7 years ago #

    In theory, could i just edit the bbpress plugin to also work in wordpress by changing things from bb_ to wp_ so that it points to all the wordpress stuff? and then activating it in wordpress?

  25. Kahil
    Member
    Posted 7 years ago #

    Thank you everyone for your help!!! I ended up figuring it out myself through trial and error...

    here is what ended up working...

    <?php global $user_identity;
    get_currentuserinfo();

    if ($user_identity == '') {
    echo('Welcome Guest');
    } else {
    echo('<img src="/forum/avatars/' . strtolower($user_identity) . '.jpg" alt="avatar" />');
    }
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic