Skip to:
Content

BuddyPress.org

Ticket #5202: set_group_avatar_by_group_id.php

File set_group_avatar_by_group_id.php, 1.7 KB (added by svenl77, 11 years ago)

set_group_avatar_by_group_id

Line 
1<?php
2
3// This is an example function: Set a new Group Avatar from the group id.
4
5function set_group_avatar_by_group_id($group_id, $_FILES){
6        global $bp;
7       
8        // I have to define the action here instead of in the form via a hidden field, because I like to act on other form inputs where I can not add my hidden fields
9        $_POST['action'] = 'bp_avatar_upload';
10       
11        // I need to add the group as current_group to the global $bp->groups->current_group.
12        // The function groups_avatar_upload_dir needs the current_group id in the global $bp->groups->current_group->id to work...
13        $current_group = groups_get_group(array('group_id' => $group_id));
14        $bp->groups->current_group = $current_group;
15   
16        // This is needed, otherwise there is no avatar_admin object to be filled up by the bp_core_avatar_handle_upload function
17    $bp->avatar_admin = new stdClass(); 
18   
19    if ( !empty( $_FILES ) ) {
20         
21        // Pass the file to the avatar upload handler
22        if ( bp_core_avatar_handle_upload( $_FILES, 'groups_avatar_upload_dir' ) ) {
23           
24            $bp->avatar_admin->step = 'crop-image';
25                       
26                        // Now we need to crop the image. This will also save the cropped images to the correct group folder.
27                        if ( !bp_core_avatar_handle_crop( array( 'object' => 'group', 'avatar_dir' => 'group-avatars', 'item_id' => $bp->groups->current_group->id, 'original_file' => $bp->avatar_admin->image->dir) ) )
28                bp_core_add_message( __( 'There was an error saving the group avatar, please try uploading again.', 'buddypress' ), 'error' );
29            else
30                bp_core_add_message( __( 'The group avatar was uploaded successfully!', 'buddypress' ) );
31
32        }
33    }   
34}
35?>