bp_activity_media_sideload_attachment( string $file )

Download an image from the specified URL and attach it to a post.

Description

Parameters

$file

(Required) The URL of the image to download

Return

(int|void)

Source

File: bp-activity/bp-activity-functions.php

5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
function bp_activity_media_sideload_attachment( $file ) {
    if ( empty( $file ) ) {
        return;
    }
 
    // Set variables for storage, fix file filename for query strings.
    preg_match( '/[^\?]+\.(jpe?g|jpe|gif|png|svg|bmp|mp4)\b/i', $file, $matches );
    $file_array = array();
 
    if ( empty( $matches ) ) {
        return;
    }
 
    $file_array['name'] = basename( $matches[0] );
 
    // Download file to temp location.
    $file                   = preg_replace( '/^:*?\/\//', $protocol = strtolower( substr( $_SERVER["SERVER_PROTOCOL"], 0, strpos( $_SERVER["SERVER_PROTOCOL"], '/' ) ) ) . '://', $file );
    $file_array['tmp_name'] = download_url( $file );
 
    // If error storing temporarily, return the error.
    if ( is_wp_error( $file_array['tmp_name'] ) ) {
        return;
    }
 
    // Do the validation and storage stuff.
    $id = bp_activity_media_handle_sideload( $file_array );
 
    // If error storing permanently, unlink.
    if ( is_wp_error( $id ) ) {
        return;
    }
 
    return $id;
}

Changelog

Changelog
Version Description
BuddyBoss 1.0.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.