• Resolved pasttenseofdraw

    (@pasttenseofdraw)


    Based on the documentation I understand where a user’s files go on the server. However, when a user edits their profile and replaces the file, the previous one appears to be deleted from the server. Is there any way to keep the previous files around? They don’t need to be accessible from the front end, as long as they remain on the server.

Viewing 6 replies - 1 through 6 (of 6 total)
  • missveronica

    (@missveronicatv)

    @pasttenseofdraw

    Yes, you can by using the um_can_remove_uploaded_file filter hook
    in a custom code snippet.

    You will find the filter hook in line 1404 of
    .../plugins/ultimate-member/includes/core/class-uploader.php

    Code example

    add_filter( 'um_can_remove_uploaded_file', 'um_can_remove_uploaded_file_content_moderation', 10, 3 );
    function um_can_remove_uploaded_file_content_moderation( $bool, $user_id, $str ) {
    
        if ( in_array( substr( $str, 0, 7 ), array( 'current', 'update_' ))) {
            $bool = false;
        }
    
        return $bool;
    }
    Thread Starter pasttenseofdraw

    (@pasttenseofdraw)

    Thanks for this @missveronicatv !

    I implemented your code example in the functions.php file but the same thing is happening – when I replace a file on a user profile, the old value is overwritten. I assume I need to modify the code in some way to make it work, but my PHP/WP understanding isn’t good enough to grasp it.

    missveronica

    (@missveronicatv)

    @pasttenseofdraw

    Yes, you are right the code snippet must specify which files to keep.
    Uploaded files are either named with a prefix or can be selected by the file type.

    Which files will you keep?

    Thread Starter pasttenseofdraw

    (@pasttenseofdraw)

    We would want to keep all the files that any user of a certain role type uploads. And, they can upload multiple file types (pdf, png, jpg, tif).

    To give a little more context – we’re using UM to create a registry of vendors for an organization, and the vendors apply for their own accounts. They need to upload certain documentation to their account, and if they update those files, we want to make sure the original files are not lost, in case we need to do an audit in the future.

    I’ve already set it so that the user can’t edit the fields on their profile, but they may need to update the documentation – so an admin can still edit the file upload in the field, and if they do, the previous file gets overwritten.

    It’s fine that only the latest iteration is displayed on the user profile on the front end, but we would want to be able to retrieve the previous file from the server via sFTP if needed.

    missveronica

    (@missveronicatv)

    @pasttenseofdraw

    This code snippet will save all files except updates of Profile and Cover photos
    if the User has the Vendor UM role or is a site Administrator.

    Edit the um_vendor if required.

    add_filter( 'um_can_remove_uploaded_file', 'um_can_remove_uploaded_file_save_vendor_files', 10, 3 );
    function um_can_remove_uploaded_file_save_vendor_files( $bool, $user_id, $str ) {
        if ( substr( $str, 0, 13 ) != 'profile_photo' && substr( $str, 0, 11 ) != 'cover_photo') {        
            $role_id = UM()->roles()->get_priority_user_role( $user_id );
            if ( $role_id == 'um_vendor' || current_user_can( 'administrator' )) {
                $bool = false;
            }
        }
        return $bool;
    }

    You install the code snippet by adding it
    to your active theme’s functions.php file
    or use the “Code Snippets” Plugin

    https://wordpress.org/plugins/code-snippets/

    Thread Starter pasttenseofdraw

    (@pasttenseofdraw)

    @missveronicatv – this works perfectly! Thank you so much again!!

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Keep file uploads after replacement’ is closed to new replies.