Skip to content

Instantly share code, notes, and snippets.

@imath
Created January 31, 2017 11:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save imath/f890824ba4b1ae43a7aed82b789e3e70 to your computer and use it in GitHub Desktop.
Save imath/f890824ba4b1ae43a7aed82b789e3e70 to your computer and use it in GitHub Desktop.
This is a reply to my contact form. Question was: how is it possible to include the file's owner display name in BuddyDrive loops.
<?php
/**
* @see https://codex.buddypress.org/themes/bp-custom-php/
* to learn more about the bp-custom.php file
*/
// Exit if accessed directly
defined( 'ABSPATH' ) || exit;
/**
* Edit each fetched Buddydrive to include the file owner display name.
*/
function sergio_add_display_name( $item = array() ) {
// Only do this on front-end.
if ( empty( $item['user_id'] ) || ! is_buddypress() ) {
return $item;
}
// Add the display name to the fetched item.
$item['user_display_name'] = bp_core_get_user_displayname( $item['user_id'] );
/**
* If we have a display name let's prepend the user avatar to it
* and remove this avatar form the item actions.
*/
if ( ! empty( $item['user_display_name'] ) ) {
$item['user_display_name'] = str_replace(
'width="50" height="50"',
'width="20" height="20" style="vertical-align: middle; border-radius: 50%"',
$item['user_avatar']
) . ' ' . $item['user_display_name'];
// Unset the user avatar now it's prepended to display name.
unset( $item['user_avatar'] );
}
return $item;
}
add_filter( 'buddydrive_prepare_for_js', 'sergio_add_display_name', 10, 2 );
/**
* Insert the display name after the file description
*/
function sergio_add_display_name_in_js_template() {
// Only do this on front-end.
if ( ! is_buddypress() ) {
return;
}
// Insert some code into the Javascript template.
?>
<# if ( data.user_display_name ) { #>
<div class="user-displayname" style="position: absolute; bottom: 15px; float:left; width:auto; height: 10px; line-height: 10px;">
<b><?php esc_html_e( 'Uploaded by:', 'sergio-textdomain' ); ?></b>
<a style="text-decoration: none" href="{{data.user_link}}" data-user-id="{{data.user_id}}">{{{data.user_display_name}}}</a>
</div>
<# } #>
<?php
}
add_action( 'buddydrive_js_template_before_actions', 'sergio_add_display_name_in_js_template' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment