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()

Return

(string|bool) The url or the path to the attachment, false otherwise

Source

File: bp-core/bp-core-attachments.php

474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
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

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.