WordPress.org

Ready to get started?Download WordPress

Forums

BuddyPress Activity Plus
[resolved] Deleting activity does not delete uploaded images (7 posts)

  1. David Hunt
    Member
    Posted 1 year ago #

    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?

    Best,

    David

    http://wordpress.org/extend/plugins/buddypress-activity-plus/

  2. David Hunt
    Member
    Posted 1 year ago #

    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) {
    						unlink(BPFB_BASE_IMAGE_DIR.$image);
    						$image_fn = substr($image, 0, strrpos($image, '.'));
    						$image_ext = substr($image, strrpos($image, '.') + 1);
    						unlink(BPFB_BASE_IMAGE_DIR.$image_fn.'-bpfbt.'.$image_ext);
    					}
    				}
    			}
    		}
    	}
    }
    add_action( 'bp_before_activity_delete', 'delete_activity_images');

    Any suggestions on improving efficiency would be very welcome.

  3. yzqiang
    Member
    Posted 1 year ago #

    Thanks for your sharing.

  4. fernon
    Member
    Posted 1 year ago #

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

  5. David Hunt
    Member
    Posted 1 year ago #

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

  6. fernon
    Member
    Posted 1 year ago #

    @davidhunt Thanks!

  7. fernon
    Member
    Posted 1 year ago #

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

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic