bp_core_delete_existing_avatar( array|string $args = '' )

Delete an existing avatar.

Description

Parameters

$args

(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

767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
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.