[教程] WordPress上传图片时自动将图片重命名为文章标题

查看: 435|回复: 1
wpocn 发表于 2023-3-3 11:07:20

为 WordPress 文章上传添加图片时自动重命名图片名称,可以简化操作过程序,之前的文章可以用时间或者MD5生成数字重命名所有媒体文件。

这里再分享一段,在文章编辑时上传添加图片,自动将图片重命名为文章标题,并自动填充图片ATL、说明、替代文本、描述等相关信息。


将下面代码添加到当前主题函数模板functions.php中:

  1. function file_renamer( $filename ) {
  2.         $info = pathinfo( $filename );
  3.         $ext  = empty( $info['extension'] ) ? '' : '.' . $info['extension'];
  4.         $name = basename( $filename, $ext );
  5.         if( $post_id = array_key_exists( "post_id", $_POST) ? $_POST["post_id"] : null ) {
  6.                 if($post = get_post($post_id)) {
  7.                         return $post->post_title . $ext;
  8.                 }
  9.         }

  10.         $my_image_title = $post;
  11.         $file['name'] = $my_image_title  . - uniqid() . $ext; // uniqid method
  12.         // $file['name'] = md5($name) . $ext; // md5 method
  13.         // $file['name'] = base64_encode( $name ) . $ext; // base64 method
  14.         return $filename;
  15. }

  16. add_filter( 'sanitize_file_name', 'file_renamer', 10, 1 );

  17. // 上传时自动设置图像标题、替代文本、标题和描述
  18. add_action( 'add_attachment', 'my_set_image_meta_upon_image_upload' );
  19. function my_set_image_meta_upon_image_upload( $post_ID ) {

  20.         // 检查上传的文件是否是图片
  21.         if ( wp_attachment_is_image( $post_ID ) ) {

  22.                 if( isset( $_REQUEST['post_id'] ) ) {
  23.                         $post_id = $_REQUEST['post_id'];
  24.                 } else {
  25.                         $post_id = false;
  26.                 }

  27.                 if ( $post_id != false ) {
  28.                         $my_image_title = get_the_title( $post_id );
  29.                 } else {
  30.                         $my_image_title = get_post( $post_ID )->post_title;
  31.                 }

  32.                 // 清理标题**殊字符
  33.                 $my_image_title = preg_replace( '%\s*[-_\s]+\s*%', ' ',  $my_image_title );

  34.                 // 将第一个字母大写
  35.                 $my_image_title = ucwords( strtolower( $my_image_title ) );

  36.                 // 创建包含标题、说明、描述的数组
  37.                 $my_image_meta = array(
  38.                         'ID'        => $post_ID,             // ID
  39.                         'post_title'    => $my_image_title,  // 图像标题
  40.                         'post_excerpt'  => $my_image_title,  // 图像说明
  41.                         'post_content'  => $my_image_title,  // 图像描述
  42.                 );

  43.                 // 添加图像 Alt
  44.                 update_post_meta( $post_ID, '_wp_attachment_image_alt', $my_image_title );

  45.                 // 添加标题、说明、描述
  46.                 wp_update_post( $my_image_meta );
  47.         }
  48. }
复制代码

提示:上面的方法只适合在文章编辑页面使用,如果在媒体库上传无效。另外,图片名称为中文貌似有的主机环境并不支持。

crll 发表于 2023-3-3 11:42:39
你可以放个链接,但是别这样通篇文章超链接。这样不好。
博主论坛 bzlt.net
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关于本站联系我们FAQ友情链接免责声明生存法则

Build with for "make" Copyright © 2020-2022. Powered by Discuz! GMT+8, 2024-4-20 06:42

快速回复 返回顶部 返回列表