bp_attachments_get_attachment

Get the url or the path for a type of attachment.

Description

(string|bool) bp_attachments_get_attachment( (string) $data = 'url', (array) $args = array() ); 

Returns (string|bool)

The url or the path to the attachment, false otherwise

Parameters (2)

0. $data — Optional. (string) => 'url'
Whether to get the url or the path.
1. $args — Optional. (array) => array()
The args.

Options

  • object_dir (string) => to members

    The object dir (eg: members/groups).

  • item_id (int) => to current user

    The object id (eg: a user or a group id).

  • type (string) => 'cover-image'

    The type of the attachment which is also the subdir where files are saved.

array(

    /**
     * The object dir (eg: members/groups).
     *
     * @type string
     * @default to members
     */
    'object_dir' => to members,

    /**
     * The object id (eg: a user or a group id).
     *
     * @type int
     * @default to current user
     */
    'item_id' => to current user,

    /**
     * The type of the attachment which is also the subdir where files are saved.
     *
     * @type string
     * @default 'cover-image'
     */
    'type' => 'cover-image'
);        


Usage

  1. if ( !function_exists( 'bp_attachments_get_attachment' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-core/bp-core-attachments.php'; 
  3.  
  4. // whether to get the url or the path. 
  5. $data = 'url'; 
  6.  
  7. // The args. 
  8. $args = array( 
  9. 'object_dir' => to members, 
  10. 'item_id' => to current user, 
  11. 'type' => 'cover-image' 
  12. ); 
  13.  
  14. // NOTICE! Understand what this does before running. 
  15. $result = bp_attachments_get_attachment($data, $args); 
  16.  

Defined (1)

The function is defined in the following location(s).

/bp-core/bp-core-attachments.php  
  1. function bp_attachments_get_attachment( $data = 'url', $args = array() ) { 
  2. // Default value. 
  3. $attachment_data = false; 
  4.  
  5. $r = bp_parse_args( $args, array( 
  6. 'object_dir' => 'members',  
  7. 'item_id' => bp_loggedin_user_id(),  
  8. 'type' => 'cover-image',  
  9. 'file' => '',  
  10. ), 'attachments_get_attachment_src' ); 
  11.  
  12. /** 
  13. * Filters whether or not to handle fetching a BuddyPress image attachment. 
  14. * If you want to override this function, make sure you return false. 
  15. * @since 2.5.1 
  16. * @param null|string $value If null is returned, proceed with default behaviour. Otherwise, value returned verbatim. 
  17. * @param array $r { 
  18. * @type string $object_dir The object dir (eg: members/groups). Defaults to members. 
  19. * @type int $item_id The object id (eg: a user or a group id). Defaults to current user. 
  20. * @type string $type The type of the attachment which is also the subdir where files are saved. 
  21. * Defaults to 'cover-image' 
  22. * @type string $file The name of the file. 
  23. * } 
  24. */ 
  25. $pre_filter = apply_filters( 'bp_attachments_pre_get_attachment', null, $r ); 
  26. if ( $pre_filter !== null ) { 
  27. return $pre_filter; 
  28.  
  29. // Get BuddyPress Attachments Uploads Dir datas. 
  30. $bp_attachments_uploads_dir = bp_attachments_uploads_dir_get(); 
  31.  
  32. // The BP Attachments Uploads Dir is not set, stop. 
  33. if ( ! $bp_attachments_uploads_dir ) { 
  34. return $attachment_data; 
  35.  
  36. $type_subdir = $r['object_dir'] . '/' . $r['item_id'] . '/' . $r['type']; 
  37. $type_dir = trailingslashit( $bp_attachments_uploads_dir['basedir'] ) . $type_subdir; 
  38.  
  39. if ( ! is_dir( $type_dir ) ) { 
  40. return $attachment_data; 
  41.  
  42. if ( ! empty( $r['file'] ) ) { 
  43. if ( ! file_exists( trailingslashit( $type_dir ) . $r['file'] ) ) { 
  44. return $attachment_data; 
  45.  
  46. if ( 'url' === $data ) { 
  47. $attachment_data = trailingslashit( $bp_attachments_uploads_dir['baseurl'] ) . $type_subdir . '/' . $r['file']; 
  48. } else { 
  49. $attachment_data = trailingslashit( $type_dir ) . $r['file']; 
  50.  
  51. } else { 
  52. $file = false; 
  53.  
  54. // Open the directory and get the first file. 
  55. if ( $att_dir = opendir( $type_dir ) ) { 
  56.  
  57. while ( false !== ( $attachment_file = readdir( $att_dir ) ) ) { 
  58. // Look for the first file having the type in its name. 
  59. if ( false !== strpos( $attachment_file, $r['type'] ) && empty( $file ) ) { 
  60. $file = $attachment_file; 
  61. break; 
  62.  
  63. if ( empty( $file ) ) { 
  64. return $attachment_data; 
  65.  
  66. if ( 'url' === $data ) { 
  67. $attachment_data = trailingslashit( $bp_attachments_uploads_dir['baseurl'] ) . $type_subdir . '/' . $file; 
  68. } else { 
  69. $attachment_data = trailingslashit( $type_dir ) . $file; 
  70.  
  71. return $attachment_data;