bp_media_add( array|string $args = '' )

Add an media item.

Description

Parameters

$args

(Optional) An array of arguments.

  • 'id'
    (int|bool) Pass an media ID to update an existing item, or false to create a new item. Default: false.
  • 'blog_id'
    (int|bool) ID of the blog Default: current blog id.
  • 'attchment_id'
    (int|bool) ID of the attachment Default: false
  • 'user_id'
    (int|bool) Optional. The ID of the user associated with the activity item. May be set to false or 0 if the item is not related to any user. Default: the ID of the currently logged-in user.
  • 'title'
    (string) Optional. The title of the media item.
  • 'album_id'
    (int) Optional. The ID of the associated album.
  • 'group_id'
    (int) Optional. The ID of a associated group.
  • 'activity_id'
    (int) Optional. The ID of a associated activity.
  • 'privacy'
    (string) Optional. Privacy of the media Default: public
  • 'menu_order'
    (int) Optional. Menu order the media Default: false
  • 'date_created'
    (string) Optional. The GMT time, in Y-m-d h:i:s format, when the item was recorded. Defaults to the current time.
  • 'error_type'
    (string) Optional. Error type. Either 'bool' or 'wp_error'. Default: 'bool'.

Default value: ''

Return

(WP_Error|bool|int) The ID of the media on success. False on error.

Source

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

function bp_media_add( $args = '' ) {

	$r = bp_parse_args( $args, array(
		'id'            => false,                   // Pass an existing media ID to update an existing entry.
		'blog_id'       => get_current_blog_id(),   // Blog ID
		'attachment_id' => false,                   // attachment id.
		'user_id'       => bp_loggedin_user_id(),   // user_id of the uploader.
		'title'         => '',                      // title of media being added.
		'album_id'      => false,                   // Optional: ID of the album.
		'group_id'      => false,                   // Optional: ID of the group.
		'activity_id'   => false,                   // The ID of activity.
		'privacy'       => 'public',                // Optional: privacy of the media e.g. public.
		'menu_order'    => 0,                       // Optional:  Menu order.
		'date_created'  => bp_core_current_time(),  // The GMT time that this media was recorded
		'error_type'    => 'bool'
	), 'media_add' );

	// Setup media to be added.
	$media                = new BP_Media( $r['id'] );
	$media->blog_id       = $r['blog_id'];
	$media->attachment_id = $r['attachment_id'];
	$media->user_id       = (int) $r['user_id'];
	$media->title         = $r['title'];
	$media->album_id      = (int) $r['album_id'];
	$media->group_id      = (int) $r['group_id'];
	$media->activity_id   = (int) $r['activity_id'];
	$media->privacy       = $r['privacy'];
	$media->menu_order    = $r['menu_order'];
	$media->date_created  = $r['date_created'];
	$media->error_type    = $r['error_type'];

	// groups media always have privacy to `grouponly`
	if ( ! empty( $media->group_id ) ) {
		$media->privacy = 'grouponly';
	}

	$save = $media->save();

	if ( 'wp_error' === $r['error_type'] && is_wp_error( $save ) ) {
		return $save;
	} elseif ('bool' === $r['error_type'] && false === $save ) {
		return false;
	}

	/**
	 * Fires at the end of the execution of adding a new media item, before returning the new media item ID.
	 *
	 * @since BuddyBoss 1.0.0
	 *
	 * @param array $r Array of parsed arguments for the media item being added.
	 */
	do_action( 'bp_media_add', $r );

	return $media->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.