bp_attachments_get_attachment( string $data = 'url', array $args = array() )
Get the url or the path for a type of attachment.
Description
Parameters
- $data
-
(Optional) whether to get the url or the path.
Default value: 'url'
- $args
-
(Optional)
- 'object_dir'
(string) The object dir (eg: members/groups). Defaults to members. - 'item_id'
(int) The object id (eg: a user or a group id). Defaults to current user. - 'type'
(string) The type of the attachment which is also the subdir where files are saved. Defaults to 'cover-image' - 'file'
(string) The name of the file.
Default value: array()
- 'object_dir'
Return
(string|bool) The url or the path to the attachment, false otherwise
Source
File: bp-core/bp-core-attachments.php
function bp_attachments_get_attachment( $data = 'url', $args = array() ) { // Default value. $attachment_data = false; $r = bp_parse_args( $args, array( 'object_dir' => 'members', 'item_id' => bp_loggedin_user_id(), 'type' => 'cover-image', 'file' => '', ), 'attachments_get_attachment_src' ); /** * Filters whether or not to handle fetching a BuddyPress image attachment. * * If you want to override this function, make sure you return false. * * @since BuddyPress 2.5.1 * * @param null|string $value If null is returned, proceed with default behaviour. Otherwise, value returned verbatim. * @param array $r { * @type string $object_dir The object dir (eg: members/groups). Defaults to members. * @type int $item_id The object id (eg: a user or a group id). Defaults to current user. * @type string $type The type of the attachment which is also the subdir where files are saved. * Defaults to 'cover-image' * @type string $file The name of the file. * } */ $pre_filter = apply_filters( 'bp_attachments_pre_get_attachment', null, $r ); if ( $pre_filter !== null ) { return $pre_filter; } // Get BuddyPress Attachments Uploads Dir datas. $bp_attachments_uploads_dir = bp_attachments_uploads_dir_get(); // The BP Attachments Uploads Dir is not set, stop. if ( ! $bp_attachments_uploads_dir ) { return $attachment_data; } $type_subdir = $r['object_dir'] . '/' . $r['item_id'] . '/' . $r['type']; $type_dir = trailingslashit( $bp_attachments_uploads_dir['basedir'] ) . $type_subdir; if ( 1 === validate_file( $type_dir ) || ! is_dir( $type_dir ) ) { return $attachment_data; } if ( ! empty( $r['file'] ) ) { if ( ! file_exists( trailingslashit( $type_dir ) . $r['file'] ) ) { return $attachment_data; } if ( 'url' === $data ) { $attachment_data = trailingslashit( $bp_attachments_uploads_dir['baseurl'] ) . $type_subdir . '/' . $r['file']; } else { $attachment_data = trailingslashit( $type_dir ) . $r['file']; } } else { $file = false; // Open the directory and get the first file. if ( $att_dir = opendir( $type_dir ) ) { while ( false !== ( $attachment_file = readdir( $att_dir ) ) ) { // Look for the first file having the type in its name. if ( false !== strpos( $attachment_file, $r['type'] ) && empty( $file ) ) { $file = $attachment_file; break; } } } if ( empty( $file ) ) { return $attachment_data; } if ( 'url' === $data ) { $attachment_data = trailingslashit( $bp_attachments_uploads_dir['baseurl'] ) . $type_subdir . '/' . $file; } else { $attachment_data = trailingslashit( $type_dir ) . $file; } } return $attachment_data; }
Changelog
Version | Description |
---|---|
BuddyPress 2.4.0 | Introduced. |
Questions?
We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.