Reputation: 583
when my users upload a file, I need to rename it using a specific user_meta value. So, using wp_handle_upload
I've set a callback function for $upload_overrides
like this:
$upload_overrides = array( 'test_form' => false, 'unique_filename_callback' => 'change_document_name' );
and my callback function is
function change_document_name($dir, $name, $ext){
global $current_user;
$doc_type = get_user_meta($current_user->ID, 'document_type', true);
return $doc_type . '_mydoc' . $ext;
}
Now as you can see, we are talking about users documents so I need to rename them according to the document type they've uploaded. For example, if they've uploaded a "passport" and selected the document type (of course), I should get the user_meta
'document_type', use it as prefix and place it in front of the filename uploaded, outputting something like
Of course my function doesn't work and I don't understand why it doesn't take the global $current_user or at least if there is some other method to accomplish this.
Many thanks.
EDIT
To explain it better, my fault, the function change_document_name()
does rename the file like so:
This means that the function is correctly called and runs, except for the first part ignoring $doc_type
variable. For this reason I suppose that the $current_user it's not working. My complete code to upload the file is the following:
if(!empty($_FILES['docfile'])):
require_once(ABSPATH . "wp-admin" . '/includes/file.php');
$upload_overrides = array( 'test_form' => false, 'unique_filename_callback' => 'change_document_name' );
add_filter('upload_dir', 'my_user_folder'); //A documents custom folder
$uploaded_file = wp_handle_upload($_FILES['docfile'], $upload_overrides);
remove_filter( 'upload_dir', 'my_user_folder' );
$doc_file_loc = $uploaded_file['file'];
$doc_file_title = $_FILES['docfile']['name'];
$doc_file_arr = wp_check_filetype(basename($_FILES['docfile']['name']));
$doc_file_type = $doc_file_arr['type'];
$doc_file_att = array(
'post_mime_type' => $doc_file_type,
'post_title' => addslashes($doc_file_title),
'post_content' => '',
'post_status' => 'inherit',
'post_parent' => 0,
'post_author' => $uid
);
require_once(ABSPATH . "wp-admin" . '/includes/image.php');
$doc_file_id = wp_insert_attachment( $doc_file_att, $doc_file_loc, 0 );
$doc_file_url = wp_get_attachment_url( $doc_file_id );
update_user_meta($uid,'document_file', $doc_file_url);
endif;
The hook 'unique_filename_callback'
is used according to the codex here https://developer.wordpress.org/reference/functions/wp_unique_filename/
Upvotes: 0
Views: 1028
Reputation: 1392
I'm not sure why the global isn't returning the user. It should be. But try get_current_user_id()
and see if it works:
function change_document_name( $dir, $name, $ext ){
if ( ! is_user_logged_in() ) {
error_log( "User not logged in." );
return;
}
$user_id = get_current_user_id();
// Uncomment to see if there is any value
// var_dump( $user_id );
$doc_type = get_user_meta( $user_id, 'document_type', true );
// Uncomment to see if there is any value
// var_dump( $doc_type );
if ( ! $doc_type ) {
error_log( "There is no doc type set for the current user with id $user_id" );
return;
}
return $doc_type . '_mydoc' . $ext;
}
I have added some var_dumps in there you can use to see what values are being returned, or you can debug if you have xdebug set up. But this should give you what you need. You can also remove the error logging if you don't want to log those errors. They are there so you can check the site logs and see what is in them.
Upvotes: 2