bp_core_delete_existing_avatar( array|string $args = '' )

Delete an existing avatar.

Description

Parameters

$args

(array|string) (Optional) Array of function parameters.

  • 'item_id'
    (bool|int) ID of the item whose avatar you're deleting. Defaults to the current item of type $object.
  • 'object'
    (string) Object type of the item whose avatar you're deleting. 'user', 'group', 'blog', or custom. Default: 'user'.
  • 'avatar_dir'
    (bool|string) Subdirectory where avatar is located. Default: false, which falls back on the default location corresponding to the $object.

Default value: ''

Return

(bool) True on success, false on failure.

Source

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

function bp_core_delete_existing_avatar( $args = '' ) {

	$defaults = array(
		'item_id'    => false,
		'object'     => 'user', // User OR group OR blog OR custom type (if you use filters).
		'avatar_dir' => false
	);

	$args = wp_parse_args( $args, $defaults );
	extract( $args, EXTR_SKIP );

	/**
	 * Filters whether or not to handle deleting an existing avatar.
	 *
	 * If you want to override this function, make sure you return false.
	 *
	 * @since BuddyPress 2.5.1
	 *
	 * @param bool  $value Whether or not to delete the avatar.
	 * @param array $args {
	 *     Array of function parameters.
	 *
	 *     @type bool|int    $item_id    ID of the item whose avatar you're deleting.
	 *                                   Defaults to the current item of type $object.
	 *     @type string      $object     Object type of the item whose avatar you're
	 *                                   deleting. 'user', 'group', 'blog', or custom.
	 *                                   Default: 'user'.
	 *     @type bool|string $avatar_dir Subdirectory where avatar is located.
	 *                                   Default: false, which falls back on the default location
	 *                                   corresponding to the $object.
	 * }
	 */
	if ( ! apply_filters( 'bp_core_pre_delete_existing_avatar', true, $args ) ) {
		return true;
	}

	if ( empty( $item_id ) ) {
		if ( 'user' == $object )
			$item_id = bp_displayed_user_id();
		elseif ( 'group' == $object )
			$item_id = buddypress()->groups->current_group->id;
		elseif ( 'blog' == $object )
			$item_id = $current_blog->id;

		/** This filter is documented in bp-core/bp-core-avatars.php */
		$item_id = apply_filters( 'bp_core_avatar_item_id', $item_id, $object );

		if ( !$item_id ) return false;
	}

	if ( empty( $avatar_dir ) ) {
		if ( 'user' == $object )
			$avatar_dir = 'avatars';
		elseif ( 'group' == $object )
			$avatar_dir = 'group-avatars';
		elseif ( 'blog' == $object )
			$avatar_dir = 'blog-avatars';

		/** This filter is documented in bp-core/bp-core-avatars.php */
		$avatar_dir = apply_filters( 'bp_core_avatar_dir', $avatar_dir, $object );

		if ( !$avatar_dir ) return false;
	}

	/** This filter is documented in bp-core/bp-core-avatars.php */
	$avatar_folder_dir = apply_filters( 'bp_core_avatar_folder_dir', bp_core_avatar_upload_path() . '/' . $avatar_dir . '/' . $item_id, $item_id, $object, $avatar_dir );

	if ( !file_exists( $avatar_folder_dir ) )
		return false;

	if ( $av_dir = opendir( $avatar_folder_dir ) ) {
		while ( false !== ( $avatar_file = readdir($av_dir) ) ) {
			if ( ( preg_match( "/-bpfull/", $avatar_file ) || preg_match( "/-bpthumb/", $avatar_file ) ) && '.' != $avatar_file && '..' != $avatar_file )
				@unlink( $avatar_folder_dir . '/' . $avatar_file );
		}
	}
	closedir($av_dir);

	@rmdir( $avatar_folder_dir );

	/**
	 * Fires after deleting an existing avatar.
	 *
	 * @since BuddyPress 1.1.0
	 *
	 * @param array $args Array of arguments used for avatar deletion.
	 */
	do_action( 'bp_core_delete_existing_avatar', $args );

	return true;
}

Changelog

Changelog
Version Description
BuddyPress 1.1.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.