WordPress 3.0 media uploader alters my image filename

I've run into a problem with WordPress 3.0

I preface my image files with an underscore character (_somefile.jpg) to allow me to flag them for specific uses vs images that don't have the underscore.

However, I've just found that the media uploader in WP 3.0 strips these underscores from the file name. At first I thought it was just renaming the wordpress title for the image but I've verified it in FTP and its actually renaming the file itself.

Is there a setting I can toggle via script to disable this filename editing?

Answers


Function sanitize_file_name() in wp-includes/formatting.php, line 681:

$filename = trim($filename, '.-_');

From the function documentation: "Trim period, dash and underscore from beginning and end of filename."

There is a filter run after this trim() named sanitize_file_name. This code will fix your problem (untested):

function preserve_leading_underscore( $filename, $filename_raw ) {
    if( "_" == substr($filename_raw, 0, 1) ) {
        $filename = "_" . $filename;
    }

    return $filename;
}
add_filter('sanitize_file_name', 'preserve_leading_underscore', 10, 2);

here's the same thing in a one-liner (TESTED!):

add_filter('sanitize_file_name',create_function('$f,$fr','return preg_match("`^_`",$fr) ? "_".$f : $f;'),10,2);

i confirmed, at least, that WordPress 3.5.1 is stripping leading underscores and that the addition of this filter preserved the leading underscores.


Need Your Help

FancyBox 2.0 afterClose event

fancybox jquery

I have script fancybox like this

Cholesky decomposition in python, recursive

python math

I am trying to make a recursive program to compute the Cholesky factorization, but the output is not in lower triangular form. How can I change this to compute it correctly?