{"id":45571,"date":"2022-05-21T06:16:55","date_gmt":"2022-05-21T06:16:55","guid":{"rendered":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/?p=45571"},"modified":"2026-10-10T18:48:03","modified_gmt":"2026-10-10T18:48:03","slug":"convert-wordpress-image-filename-to-lowercase","status":"publish","type":"post","link":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/convert-wordpress-image-filename-to-lowercase\/","title":{"rendered":"Lowercase Uploaded File Names in WordPress"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">WordPress keeps the capital letters in uploaded file names, so <code>Team-Photo.JPG<\/code> stays <code>Team-Photo.JPG<\/code> in the media library and in its URL. This snippet makes WordPress lowercase uploaded file names automatically, for every file type or only for images.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Lowercase names give predictable URLs. Most Linux servers treat <code>Photo.jpg<\/code> and <code>photo.jpg<\/code> as two different files, so a link typed in lowercase to an uploaded <code>Photo.JPG<\/code> returns a 404.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The snippet<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">&lt;?php\n\/**\n * Lowercase the names of files uploaded to WordPress.\n *\/\n\n\/\/ ===== Config =====\nfunction dpc_lowercase_filenames_config() {\n\t$config = array(\n\t\t\/\/ false = every file type. true = images only (jpg, png, gif, webp, avif, heic, svg \u2026).\n\t\t'images_only' =&gt; false,\n\t);\n\t\/\/ ===== End of config =====\n\n\treturn (array) apply_filters( 'dpc_lowercase_filenames_config', $config );\n}\n\nadd_filter( 'sanitize_file_name', 'dpc_lowercase_filename' );\n\nfunction dpc_lowercase_filename( $filename ) {\n\t$config = dpc_lowercase_filenames_config();\n\n\tif ( ! empty( $config&#91;'images_only'&#93; ) ) {\n\t\t$types  = wp_get_ext_types();\n\t\t$images = array_merge( $types&#91;'image'&#93; ?? array(), array( 'svg' ) );\n\t\t$ext    = strtolower( pathinfo( $filename, PATHINFO_EXTENSION ) );\n\n\t\tif ( ! in_array( $ext, $images, true ) ) {\n\t\t\treturn $filename;\n\t\t}\n\t}\n\n\treturn function_exists( 'mb_strtolower' ) ? mb_strtolower( $filename, 'UTF-8' ) : strtolower( $filename );\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">How it works<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Every upload path in WordPress (the media library, the block editor, the REST API, <code>media_sideload_image()<\/code>) picks the final name with <code>wp_unique_filename()<\/code>, which runs the name through <code>sanitize_file_name()<\/code>. The snippet hooks the <code>sanitize_file_name<\/code> filter, so all of them are covered.<\/li>\n\n\n<li>The extension is lowercased too: <code>My-Photo.JPG<\/code> becomes <code>my-photo.jpg<\/code>.<\/li>\n\n\n<li>WordPress already turns accented letters into plain ones (<code>\u010caj.png<\/code> becomes <code>Caj.png<\/code>, then <code>caj.png<\/code>). For scripts it keeps, such as Cyrillic, <code>mb_strtolower()<\/code> lowercases them too: <code>\u0424\u041e\u0422\u041e.png<\/code> becomes <code>\u0444\u043e\u0442\u043e.png<\/code>. Without the mbstring extension it falls back to <code>strtolower()<\/code>.<\/li>\n\n\n<li>The name is lowercased before WordPress checks for an existing file. Uploading <code>Photo.jpg<\/code> after <code>photo.jpg<\/code> gives <code>photo-1.jpg<\/code> instead of overwriting the first file.<\/li>\n\n\n<li>With <code>images_only<\/code> set to <code>true<\/code> the extension decides: WordPress&#8217;s own image list from <code>wp_get_ext_types()<\/code> (jpg, png, gif, webp, avif, heic and more) plus <code>svg<\/code>. Other files, such as PDFs, keep their name.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Add it with Scripts Organizer<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/dplugins.com\/downloads\/scripts-organizer\/\">Scripts Organizer<\/a> ships this snippet in its One-Click Import library as <strong>Lowercase filenames for upload files<\/strong>. To add it by hand, go to <strong>Code Blocks \u2192 Add New<\/strong> and give the block a name. Set <strong>Trigger location<\/strong> to <strong>Everywhere<\/strong>, tick <strong>PHP<\/strong> under <strong>Script location<\/strong>, paste the code into the PHP editor, edit the config and publish. Leave <strong>Action\/Hook<\/strong> empty.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Use <strong>Everywhere<\/strong>. Uploads happen in wp-admin and through REST API requests from the block editor, and <strong>Conditions<\/strong> only runs on front-end pages, so the filter would never be added when a file is uploaded.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The same code also works unchanged in a small plugin or an mu-plugin.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Customise it<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Images only: set <code>'images_only' =&gt; true<\/code>.<\/li>\n\n\n<li>SVG uploads are blocked by default; to allow them safely see <a href=\"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/allow-svg-uploads-wordpress-admins-only\/\">Allow SVG Uploads in WordPress for Admins Only<\/a>. This snippet then lowercases those names as well.<\/li>\n\n\n<li>Change the setting from other code with the <code>dpc_lowercase_filenames_config<\/code> filter.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Gotchas<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Only new uploads change. Files already in the media library keep their names and URLs.<\/li>\n\n\n<li><code>sanitize_file_name()<\/code> is also used by some plugins for files they write themselves, for example exports. Those names are lowercased as well.<\/li>\n\n\n<li>The attachment title still comes from the original name, so the media library shows the title as it was typed.<\/li>\n\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">The older version of this post lowercased only a fixed list of image MIME types, which contained <code>ico<\/code> instead of a MIME type and missed WebP, AVIF and SVG. It also suggested loading the code on the front end, where it never runs for uploads.<\/p>\n\n","protected":false},"excerpt":{"rendered":"<p>Make WordPress save every uploaded file with a lowercase file name, or only images, so URLs stay consistent on case-sensitive servers.<\/p>\n","protected":false},"author":2,"featured_media":46697,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"slim_seo":{"title":"Lowercase Uploaded File Names in WordPress - Code DPlugins","description":"Make WordPress save every uploaded file with a lowercase file name, or only images, so URLs stay consistent on case-sensitive servers."},"_slim_seo_primary_term_category":0,"_slim_seo_primary_term_post_tag":0,"footnotes":""},"categories":[3],"tags":[64,62,59],"dpc_library":[204],"class_list":["post-45571","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-wordpress","tag-convert","tag-filename","tag-image"],"_links":{"self":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/45571","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/comments?post=45571"}],"version-history":[{"count":1,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/45571\/revisions"}],"predecessor-version":[{"id":46696,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/45571\/revisions\/46696"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/media\/46697"}],"wp:attachment":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/media?parent=45571"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/categories?post=45571"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/tags?post=45571"},{"taxonomy":"dpc_library","embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/dpc_library?post=45571"}],"curies":[{"name":"wp","href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/api.w.org\/{rel}","templated":true}]}}