Support » Plugin: User Submitted Posts » Image file names with foreign characters

  • Resolved strx

    (@strx)


    Thanks for a really great plugin! However I have a problem with the image upload. It occurs when an image is uploaded with a name containing the Swedish letters å, ä and ö. The image is saved but apparently with the wrong encoding, and it doesn’t show up when viewing the post. Is there a way I can check and modify the image name before upload?

Viewing 5 replies - 1 through 5 (of 5 total)
  • 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”?

    Plugin Author Jeff Starr

    (@specialk)

    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

    Plugin Author Jeff Starr

    (@specialk)

    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 1 year, 10 months ago by Jeff Starr.
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Image file names with foreign characters’ is closed to new replies.