Support

Account

Home Forums General Issues Auto shortlink for file uploads

Unread

Auto shortlink for file uploads

  • Is it possible to create a shortlink automatically for files (PDF) that are uploaded to a specific folder?

    For example, there will be a series of downloadable PDF’s with the following URL structure; https://isupportdev-v2-com.flexxstaging.com/wp-content/uploads/scanned-mail/401/Svincent.pdf

    401 is the company ID and will vary, as will the filename. Now, there is a chance there could be similar URLs and I would like to minimise any potential issues by somebody trying to ‘guess’ and accessing something they shouldn’t.

    I’m using ACF to upload the files and would like some way to create a random shortlink on upload or when the post that the file is attached to is published.

    Can this be done?

    EDIT

    Below is what I have to determine where the files are uploaded. Is there a way to hook into this?

    // Custom upload directory
    function gist_acf_upload_dir($path_info) {
        
        // Set to whatever directory you want the ACF file field to upload to
        $category = get_the_ID();
         $path_info["path"] = $_SERVER['DOCUMENT_ROOT'] . "/wp-content/uploads/scanned-mail/" . $category . "/";
         $path_info["url"] = 'https://' . $_SERVER['SERVER_NAME'] . "/wp-content/uploads/scanned-mail/" . $category . "/";
         
         return $path_info;
        
    }

    Edit 2

    Below is full code which does add a random string of characters, but I have to also output the original filename, which isn’t really ideal…

    function genRandomString() 
    {
        $length = 15;
        $characters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWZYZ";
    
        $real_string_length = strlen($characters) ;     
        $string="id";
    
        for ($p = 0; $p < $length; $p++) 
        {
            $string .= $characters[mt_rand(0, $real_string_length-1)];
        }
    
        return strtolower($string);
    }
    
    function gist_acf_upload_dir_prefilter($errors, $file, $field) {
        
        // Only allow editors and admins, change capability as you see fit
        if( !current_user_can('edit_pages') ) {
            $errors[] = 'Only Editors and Administrators may upload attachments';
        }
        
        // This filter changes directory just for item being uploaded
        add_filter('upload_dir', 'gist_acf_upload_dir');
        add_filter('wp_handle_upload_prefilter', 'rename_current_acf_upload', 11, 1);
        
    }
    
    function rename_current_acf_upload( $file ){
        $rand = genRandomString();
        $final_filename = $rand;
    
        
        $file['name'] = $final_filename . $file['name'];
        return $file;
    }
    
    // ACF hook, set to field key of your file upload field
    add_filter('acf/upload_prefilter/key=field_633af0a27092b', 'gist_acf_upload_dir_prefilter', 10, 3 );
    
    // Custom upload directory
    function gist_acf_upload_dir($path_info) {
        
        // Set to whatever directory you want the ACF file field to upload to
        $category = get_the_ID();
         $path_info["path"] = $_SERVER['DOCUMENT_ROOT'] . "/wp-content/uploads/scanned-mail/" . $category . "/";
         $path_info["url"] = 'https://' . $_SERVER['SERVER_NAME'] . "/wp-content/uploads/scanned-mail/" . $category . "/";
         
         return $path_info;
        
    }

    I’ve been looking at this – https://support.advancedcustomfields.com/forums/topic/rename-media-filename-during-upload/ – but no luck :/

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.