Wordpress似乎将上传文件中自9.1.2以来的所有下划线替换为短划线.我该如何删除它


Wordpress seems to replace all underscores with dashes since 9.1.2 in uploaded files. How can I remove this?

我注意到,自上次更新(9.1.2)以来,Wordpress似乎正在将上传文件名中的所有下划线替换为短划线。然而,我需要保留文件名的下划线。

我找到了一个我修改过的插件,但它似乎没有帮助。。请参阅下面的代码。

function mfl_make_filename_lowercase($filename) {
    $info = pathinfo($filename);
    $ext  = empty($info['extension']) ? '' : '.' . $info['extension'];
    $name = basename($filename, $ext);
    $name = preg_replace('/-/', '_', $name);
    return strtolower($name) . $ext;
}
add_filter('sanitize_file_name', 'mfl_make_filename_lowercase', 10);

我环顾四周,检查了wp-includes中的formating.php,并将preg_replace更改为"_",而不是我发现的"-"。我在wp-admin/includs中的files.php中也做了同样的操作。

我一定是错过了什么。还是根本不可能?请帮忙!

使用以下代码用下划线替换短划线:

    function image_meta_upon_upload( $post_ID ) {
        if ( wp_attachment_is_image( $post_ID ) ) {
            $my_image_title = get_post( $post_ID )->post_title;
            $my_image_title = preg_replace( '%'s*[-]+'s*%', '_',  $my_image_title );
            $my_image_meta = array(
                'ID' => $post_ID,               // Specify the image (ID) to be updated
                'post_title'    => $my_image_title,     // Set image Title to sanitized title
            );
            wp_update_post( $my_image_meta );
        }
    }
        add_action( 'add_attachment', 'image_meta_upon_upload' );

您可以将这些代码插入functions.php,但如果您想在主题更新后保留它,更好的解决方案是使用代码片段或wpcodebox等插件。

Google将-视为单词分隔符,而不是_。这是一篇解释它的文章

但如果你仍在寻找解决方案,这里是答案

查看wp-included/formating.php

在第1241行,您将获得sance_title_with_dashs函数find

$title = strtolower($title);
$title = preg_replace('/&.+?;/', '', $title); // kill entities
$title = str_replace('.', '-', $title);
$title = preg_replace('/[^%a-z0-9 _-]/', '', $title);
$title = preg_replace('/'s+/', '-', $title);
$title = preg_replace('|-+|', '-', $title);
$title = trim($title, '-');

用下划线替换短划线

请注意,在此更改之前创建的任何依赖%postname%permalink结构标记的帖子都将被破坏。

在这种情况下,你需要回去重新发布这些帖子,这样破折号就可以换成下划线了。或者只需编写一个SQL来替换它们。