开发者

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 stri开发者_运维技巧ps 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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜