将附件文件重命名为它在 Wordpress 中添加到的帖子标题 [英] Renaming attachment files to the post title it's being added to in Wordpress

查看:28
本文介绍了将附件文件重命名为它在 Wordpress 中添加到的帖子标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所说,我正在尝试将媒体库上传重命名为文件被添加到的初始帖子的标题.

Like the title says, I am trying to rename media gallery uploads to the title of the initial post the file is being added to.

我找到了以下函数并修改了它以将文件名更改为文件的作者 slug

I found the following function and modified it to change the filename to the file's author slug

// file renamer (used for EDL images)
function file_renamer( $filename ) {
  $info = pathinfo( $filename );
  $ext  = empty( $info['extension'] ) ? '' : '.' . $info['extension'];
  $name = basename( $filename, $ext );
  global $current_user;
  get_currentuserinfo();

  return   $current_user->user_login . $ext;
}

然而——这对客户来说还不够好.我还没有在网上找到任何解决方案.任何帮助将不胜感激!

However- that wasn't good enough for the client. I haven't found any solutions to this yet online. Any help would be greatly appreciated!

编辑 - 只是想补充一点,我也在为此使用高级自定义字段.下面是这个过程的其余代码,它将特定的自定义字段图像定向到某个目录-

Edit - Just want to add that I am using advanced custom fields for this as well. Here is the rest of the code for this process, which directs specific custom field images to a certain directory-

// EDL uploaded images will go to the /edl directory in uploads for these fields.
add_filter('acf/upload_prefilter/name=edl_dealer_logo', 'field_name_upload_prefilter');
add_filter('acf/upload_prefilter/name=edl_image', 'field_name_upload_prefilter');
add_filter('acf/upload_prefilter/name=edl_gallery_image', 'field_name_upload_prefilter');

function field_name_upload_prefilter($file) {
// in this filter we add a WP filter that alters the upload path
add_filter( 'sanitize_file_name', 'file_renamer', 10 );
add_filter('upload_dir', 'field_name_upload_dir');
return $file;
}
// second filter
  function field_name_upload_dir($uploads) {

  $mydir = '/edl';
  // here is where we later the path
  $uploads['path'] = $uploads['basedir'].$mydir;
  $uploads['url'] = $uploads['baseurl'].$mydir;

  $uploads['subdir'] = '/edl';
  return $uploads;
}

推荐答案

我想你可以看看 $_POST["post_ID"],使用 WP 的 get_post()code> 然后如果找到了一个帖子,调用 get $post->post_title 获取文件名.

I suppose you might be able look at $_POST["post_ID"], use WP's get_post() and then if a post has been found, call get $post->post_title for the filename.

或者 ACF 是否通过 Ajax 异步上传那些?无论如何,我相信上传时应该传输帖子ID,但是您可能需要查看浏览器的开发人员工具才能找到字段名称.

Or does ACF upload those asynchronously via Ajax? In any case, the post ID should be transmitted when uploading I believe, but you might have to look at your browser's developer tools to find out what the field name is.

以下在我的安装中有效.请注意,这将针对所有文件上传运行,因此您可能需要添加一些预防措施.

The following works in my install. Be advised that this would run for all file uploads, so you might want to add some precautions.

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;
        }
    }
    get_currentuserinfo();

    return   $current_user->user_login . $ext;
  }
add_filter( 'sanitize_file_name', 'file_renamer', 10, 1 );

这篇关于将附件文件重命名为它在 Wordpress 中添加到的帖子标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆