[wp-trac] [WordPress Trac] #37989: Unexpected change to media title behavior in WP 4.6.1

WordPress Trac noreply at wordpress.org
Thu Nov 10 05:14:40 UTC 2016


#37989: Unexpected change to media title behavior in WP 4.6.1
--------------------------------------------------+------------------------
 Reporter:  arhenderson63                         |       Owner:  joemcgill
     Type:  defect (bug)                          |      Status:  reopened
 Priority:  normal                                |   Milestone:  4.6.2
Component:  Media                                 |     Version:  4.6.1
 Severity:  major                                 |  Resolution:
 Keywords:  has-patch has-unit-tests fixed-major  |     Focuses:
--------------------------------------------------+------------------------

Comment (by arthurvkuhrmeier):

 Searching for a solution myself I stumbled upon this thread. Happy to say,
 I found a way to fix this.

 Luckily, WordPress uses two different variables to sanitize the title and
 the filename. First, let's fix the title:

 {{{#!php
 <?php
 //===== Fix sanitized Media Titles - Part I =====
 function noobclub_sanitize_title ($title, $fallback_title, $context) {
         return ( strlen($fallback_title)  ?  $fallback_title  :  $title );
 }
 add_filter('sanitize_title', 'noobclub_sanitize_title', 10, 3);
 }}}
 The $fallback_title contains the original filename. In other words, we do
 not sanitize title and filename at all! '''So, use at your own risk.'''

 Next, let's sanitize the filename. I prefer filenames in pure ASCII
 without spaces:
 {{{#!php
 <?php
 //===== Fix sanitized Media Titles - Part II =====
 function noobclub_upload_filter ($file) {
         $name = explode('.', $file['name']);
         $ext  = array_pop($name);
         $name = implode('.', $name);
         //--- If your PHP version does not support transliterator, ---
         //--- you'll have to find a different solution             ---
         $name = transliterator_transliterate('Any-Latin; Latin-ASCII;
 [\u0080-\u7fff] remove', $name);
         $name = sanitize_file_name(sanitize_title_with_dashes($name));
         $file['name'] = $name.'.'.$ext;
         return $file;
 }
 add_filter('wp_handle_upload_prefilter', 'noobclub_upload_filter');
 }}}
 Finally, I wanted the slug to be the same as the filename:
 {{{#!php
 <?php
 //===== Use media file name as slug =====
 function noobclub_attachment_slug ($data, $postarr) {
         $name = explode('/', $data['guid']);
         $name = array_pop($name);
         $name = explode('.', $name);
         $data['post_name'] = reset($name);
         return $data;
 }
 add_filter('wp_insert_attachment_data', 'noobclub_attachment_slug', 10,
 2);
 }}}
 Now, I put together a fictive filename and uploaded the file:
 {{{
 Größe? - S’il (vous) plaît. - 神保彰 - Руслана.png
 }}}
 The result is very satisfying! '''The title is unaltered, taken from the
 original file.''' The filename and the attachment page now look nice, too:
 {{{
 http://noobclub.net/wp-content/uploads/grosse-sil-vous-plait-shen-bao-
 zhang-ruslana.png
 http://noobclub.net/grosse-sil-vous-plait-shen-bao-zhang-ruslana/
 }}}
 I simply put the code in the functions.php of my theme. I hope it is
 useful for you. Enjoy!

--
Ticket URL: <https://core.trac.wordpress.org/ticket/37989#comment:63>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list