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
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
| 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.