Support » Plugin: BuddyPress Activity Plus » Deleting activity does not delete uploaded images

  • Resolved David Hunt


    I think that users would assume that, if they delete an activity update, then any photos that were uploaded within that update would also be deleted. But it seems that the files are left on the server if the activity is deleted.

    I’m working with a client for whom user privacy is a priority, and this is an essential.

    Is there any way to hook into the delete action so that the associated files are also removed?



Viewing 6 replies - 1 through 6 (of 6 total)
  • Think this does what I was asking for, if anyone else is interested:

    function delete_activity_images( $args ) {
    	// This function is designed to delete images added by
    	// BuddyPress Activity Plus (BPAP), so we first check for the constant
    	// which defines the path to uploaded images.
    	// If its not there, assume we use the default value
    	if (!defined('BPFB_BASE_IMAGE_DIR')) {
    		define('BPFB_BASE_IMAGE_DIR', $wp_upload_dir['basedir'] . '/bpfb/', true);
    	// Get the contents of the activity we are about to delete
    	global $wpdb;
    	$content = $wpdb->get_var( $wpdb->prepare( "SELECT content FROM ".$wpdb->prefix."bp_activity WHERE id = %d;", $args['id'] ) );
    	if ($content != '') {
    		// Look for the shortcode surrounding image filenames uploaded the BPAP
    		$matches = array();
    		preg_match('/\[bpfb_images\](.*?)\[\/bpfb_images\]/s', $content, $matches);
    		// If there are any images, delete each one and its thumbnail
    		if ($matches) {
    			foreach ($matches as $match) {
    				$images = array();
    				$images = explode('\n', trim(strip_tags($match)));
    				if (!empty($images)) {
    					foreach ($images as $image) {
    						$image_fn = substr($image, 0, strrpos($image, '.'));
    						$image_ext = substr($image, strrpos($image, '.') + 1);
    add_action( 'bp_before_activity_delete', 'delete_activity_images');

    Any suggestions on improving efficiency would be very welcome.

    Thanks for your sharing.

    where do I need to place this piece of code? in the functions.php?

    @angelsih: Yes, put it in your theme’s functions.php file.

    @davidhunt Thanks!

    @davidhunt btw you forgot one } at the end of the code

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Deleting activity does not delete uploaded images’ is closed to new replies.