While at it, it would be good to make sure to filter out spaces, too.
A tips is to simply urlencode whatever you get from the user.
Tor-Bjorn, can you point me to more info on this “urlencode”?
Glad to help. To modify the name (and other details, optional) of each submitted file, you can use the filter hook, usp_insert_attachment_data
. Here is complete documentation on using filter hooks, if you are unfamiliar.
@rjpivonka I’m not sure it would solve this particular situation, whether you’ll get valid filenames that way, or not. Perhaps an approach is what social networks do all the time: They just generate pseudorandom strings as filenames. Whatever they got from the user may be kept simply as meta-data.
urlencode is a PHP function: http://php.net/manual/en/function.urlencode.php
Just to follow up with this, here is an example of how to access the attachment variables for each uploaded image:
function usp_customize_attachment_data($attachment) {
/*
$attachment includes:
array(5) {
["post_mime_type"]=>
string(10) "image/jpeg"
["post_name"]=>
string(13) "example.jpg"
["post_title"]=>
string(13) "example.jpg"
["post_status"]=>
string(7) "inherit"
["guid"]=>
string(67) "https://example.com/wp-content/uploads/2018/05/example.jpg"
}
*/
// do your thing..
return $attachment;
}
add_filter('usp_insert_attachment_data', 'usp_customize_attachment_data');
I hope it helps!
-
This reply was modified 6 years, 10 months ago by
Jeff Starr.