bp_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-media/bp-media-functions.php

1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
function bp_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 );
 
    if ( ! function_exists( 'download_url' ) ) {
        require_once( ABSPATH . 'wp-admin' . '/includes/image.php' );
        require_once( ABSPATH . 'wp-admin' . '/includes/file.php' );
        require_once( ABSPATH . 'wp-admin' . '/includes/media.php' );
    }
    $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_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.