【问题标题】:Replace space with hyphen for image filename on upload上传时用连字符替换图像文件名的空格
【发布时间】:2021-10-21 12:44:01
【问题描述】:

我正在使用下面的代码(我在this post 的堆栈溢出中找到它)来自动重命名图像文件名并根据帖子标题填充 alt 和标题字段。

好消息是它正在工作,它正在做它的工作,但问题在于文件名:它使用带有空格、逗号等的帖子标题作为文件名,这真的不好。它非常适合填充标题和 alt 字段,但不适用于文件名。

所以,对于文件名,我想添加一些东西来用连字符替换空格(如果可能的话,删除潜在的逗号或其他标点符号)

我原以为add_filter( 'sanitize_file_name', 'file_renamer', 10, 1 ); 的角色可以胜任这份工作,但事实并非如此。

但由于我真的不确定自己在做什么,而且我的 PHP 知识很差,如果您能教我如何使它工作,我将不胜感激:

function file_renamer( $filename ) {
$info = pathinfo( $filename );
$ext  = empty( $info['extension'] ) ? '' : '.' . $info['extension'];
$name = basename( $filename, $ext );


if( $post_id = array_key_exists("post_id", $_POST) ? $_POST["post_id"] : null) {
    if($post = get_post($post_id)) {
        return $post->post_title . $ext;
    }
}

$my_image_title = $post;


$file['name'] = $my_image_title  . - uniqid() . $ext; // uniqid method
// $file['name'] = md5($name) . $ext; // md5 method
// $file['name'] = base64_encode($name) . $ext; // base64 method

return $filename;
}
add_filter( 'sanitize_file_name', 'file_renamer', 10, 1 );

/* Automatically set the image Title, Alt-Text, Caption & Description upon upload */

add_action( 'add_attachment', 'my_set_image_meta_upon_image_upload' );
function my_set_image_meta_upon_image_upload( $post_ID ) {

// Check if uploaded file is an image, else do nothing

if ( wp_attachment_is_image( $post_ID ) ) {
    
   // Get the parent post ID, if there is one

    if( isset($_REQUEST['post_id']) ) {
      $post_id = $_REQUEST['post_id'];
      } else {
      $post_id = false;
      }


        if ($post_id != false) {
    $my_image_title = get_the_title($post_id);
        } else {
    $my_image_title = get_post( $post_ID )->post_title;
        }
    // Sanitize the title:  remove hyphens, underscores & extra spaces:
    $my_image_title = preg_replace( '%\s*[-_\s]+\s*%', ' ',  $my_image_title );

    // Create an array with the image meta (Title, Caption, Description) to be updated
    // Note:  comment out the Excerpt/Caption or Content/Description lines if not needed
    $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
        'post_content'  => $my_image_title,     // Set image Description (Content) to sanitized title
    );

    // Set the image Alt-Text
    update_post_meta( $post_ID, '_wp_attachment_image_alt', $my_image_title );

    // Set the image meta (e.g. Title, Excerpt, Content)
    wp_update_post( $my_image_meta );

} 
}

感谢您的帮助!

【问题讨论】:

    标签: wordpress image rename filenames auto


    【解决方案1】:

    所以解决方案是在file_renamer 函数中用post_name 替换post_title

    正是这部分:更改return $post->post_title . $ext;并将其替换为return $post->post_name . $ext;

    而且它正在工作。上传时,此功能使用连字符重命名文件名,并根据帖子标题(不带连字符)填充标题、替代文本和描述字段。

    【讨论】:

      猜你喜欢
      • 2011-08-27
      • 1970-01-01
      • 1970-01-01
      • 2020-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-16
      • 1970-01-01
      相关资源
      最近更新 更多