• Hi, i want to add a timestamp to filenames of every uploaded file.
    I found this code, but it doesn’t work:

    function sanitize_file_uploads( $file ){
        $file['name'] = sanitize_file_name($file['name']);
        $file['name'] = preg_replace("/[^a-zA-Z0-9\_\-\.]/", "", $file['name']);
        $file['name'] = date('d-m-Y')."_".$file['name'];
        $file['name'] = strtolower($file['name']);
        add_filter('sanitize_file_name', 'remove_accents');
    
        return $file;
    }
    add_filter('wp_handle_upload_prefilter', 'sanitize_file_uploads');

    Can anyone help me, please?
    Best regards.

    Roberto

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    Your code works fine on my site. You may have a plugin or theme conflict. Try deactivating all plugins and switching to the twentysixteen theme. Temporarily place the timestamp code in twentysixteen’s functions.php file. You should find it works. Restore your normal theme, inserting the timestamp code if needed. Still works OK? Activate plugins one by one. When it stops working, the last activated entity is causing a conflict.

    BTW, for the remove_accents() to work as a filter callback, it should be added as the very first line.

Viewing 1 replies (of 1 total)
  • The topic ‘Add timestamp to uploaded files’ is closed to new replies.