Hello, is there a way to show the username of the member that have posted a certain media? Now the thumbs are showing in a gallery and the Title is showing under the thumb which is great. But to add “@MemberName” under the title that links to the members profile would really be helpful!
Very interesting if theres a simple solution for this, suggestions are very welcome.
Thank you!/Johan
Hi Johan,
Welcome to BuddyDev forums and thank you for using MediaPress.That will be very easy( 3-5 lines of code at most ) but I need to understand the rationale correctly.
When a user is seeing a gallery, they already know whose gallery they are seeing, isn’t that correct? So, can you please explain me the need. If it is just for vanity, I will avoid as each time we link to username, it will be a few extra calls.
Please let me know and I will post the code.
Thank you
BrajeshThanks for the quick answer!
Yes, you are right that in the members own galleries the usernames won’t be necessary.
But I’m planning to have a page that displays the recent images uploaded by members. And I also plan to have a few images on the front page saying: “20 Recent images from our members” or something like that.
On these ocarinas it would be great to have the user name showing. Does that make sense?Hi Johan,
Thank you.I will post the code with explanations then. Also, if you need any help doing a custom Media Query, please do let me know. It is very simple(just like WP_Query)
1. To get the uploader user id inside the loop, we can use the following function
$user_id = mpp_get_media_creator_id();//get the id of user who uploaded this media
If you are not inside the loop, you can pass media id or object to that function like
$media_id = 32; $user_id = mpp_get_media_creator_id( $media_id );//accepts id or MPP_Media object or attachment post object
So, we are done with the user id part.
2. In BuddyPress, there are multiple ways to link to user.
Easiest way is to use
bp_core_get_userlink( $user_id )
That gives a link to the user profile with his/her Name linked
so, we can use it like this
echo bp_core_get_userlink( mpp_get_media_creator_id() );
If you are interested in linking to the username instead( Using username as the link label instead of the full name), we will need 2 functions, one for getting url of the user and another to get the username. Those are easy and there are again multiple way
$url = bp_core_get_user_domain( $user_id );//will return a ulr like http://site.com/members/xyz/ $username = bp_core_get_username( $user_id );// returns username e.g 'admin', 'sbrajesh' etc
So, we can build a link with them like this
<a href="<?php echo bp_core_get_user_domain( $user_id );?>"><?php echo bp_core_get_username( $user_id );?></a>
That will give us a link to the username if we decide to choose this method. we can write it inside a function and pass the user id to get the link
Now, the last part is to display the link in the media loop
Here, you will need to decide whether you are going to use the shortcode or creating a custom loop yourself.
If you are using a shortcode, we can hook to action ‘mpp_after_media_item’ and echo the user link. If you are using the custom query, you can list the username where you want.
Here is an example using the hook
add_action( 'mpp_after_media_item', 'mpp_custom_add_user_link' ); function mpp_custom_add_user_link() { //you may put some conditions here like //mpp_is_single_gallery(); //mpp_is_single_media(); //to avoid showing it on single gallery/media pages $user_link = bp_core_get_userlink( mpp_get_media_creator_id() ); echo $user_link; }
Though it is possible to filter title of media using the hook ‘mpp_get_media_title’, title is not always displayed( you might have already noticed it for photos, also, titles are linked and putting a link inside link will generate invalid html ). You can put some text though.
Another action that might interest you is ‘mpp_before_media_item’, it is same as ‘mpp_after_media_item’ except that it is called before showing the media content.
Hope that helps.
Btw, For shortcodes, the equivalent hook is
mpp_before_media_shortcode_item mpp_after_media_shortcode_item
instead of
mpp_before_media_item mpp_after_media_item
The ‘mpp_before_media_item’ is used on single gallery loop. For widgets, in the same way the hooks are
mpp_before_media_widget_item mpp_after_media_widget_item
Hope that helps.
Hi again!
Thank you so much for all this. But I have to admit, some of this went straight over my head. Can you give me a quick hint on where the code goes?Hi Johan,
I am sorry about that. I thought you were going into the MediaPress APi and tried to convey that.Can you please post me how will you make listing and put a screenshot showing where you want the username to be. I will post exact code.
Thank you
BrajeshHi now it worked fine! Thanks.
So I will probably use the MP widget for media for now. But if this was enabled on the shortcode method as well it would be awesome.
Here’s an example of when widget in the front page and where I would like the member name to be.http://hectornado.se/wp-content/uploads/2015/10/test.jpg
Thank you
JohanHi Johan,
Please put this in your theme’s functions.phpadd_action( 'mpp_after_media_widget_item', 'mpp_custom_add_user_link' ); function mpp_custom_add_user_link() { //you may put some conditions here like //mpp_is_single_gallery(); //mpp_is_single_media(); //to avoid showing it on single gallery/media pages $user_link = bp_core_get_userlink( mpp_get_media_creator_id() ); echo $user_link; }
and check if ti works or not?
to add support in shortcodes too, we can add one more line:-
add_action( 'mpp_after_media_shortcode_item', 'mpp_custom_add_user_link' );
That will make it work in both.
Please check and let me know.
Hello!
The first code unfortunately gave me white page with:Parse error: syntax error, unexpected ‘function’ (T_FUNCTION) in functions.php on line 60
Tried the shortcode line alone and gave me the same error:
Parse error: syntax error, unexpected ‘?>’ in functions.php on line 62
- This reply was modified 9 years, 1 month ago by Johan Andreasson.
The topic ‘ [Resolved] Add member name (@MemberName) under title on thumbs’ is closed to new replies.