根据上载时的父帖子标题重命名附件

时间:2013-03-21 作者:schwarzgrau

我知道这项任务已经有了一些可用的功能,但它们似乎都没有按需要工作。

在我看来,最好的方法是在wp_handle_upload_prefilter 喜欢kaiser 他在吗this example. 不幸的是,只有当帖子已经保存到数据库中时,文件才能获取父帖子标题。

另一种方法是在add_attachment 喜欢Ijaashere. 然后该文件获取父帖子标题作为名称,但不会创建缩略图。以及我尝试使用wp_generate_attachment_metadata(); 创建丢失的图像大小,以无休止的循环结束(可能是因为我使用它的方式不对,但现在我有点害怕这个函数)。

如果有办法把头衔传给wp_handle_upload_prefilter 即使帖子还没有保存。

哦,顺便说一下,这是我的无穷函数,也许有人能告诉我它有什么问题。DON\'T USE THIS FUNCTION !!!

add_action(\'add_attachment\', \'fkp_rename_attacment\');
function fkp_rename_attacment($post_ID){

  $post = get_post($post_ID);
  $file = get_attached_file($post_ID);
  $path = pathinfo($file);
  $parent = get_post($post->post_parent);
  $p_author = get_the_author_meta( \'display_name\', $parent->post_author );
  $p_author_san = sanitize_title($p_author);

  $newfilename = $parent->post_name . \'-\' . $p_author_san . \'-\' . $post_ID;
  $newfile = $path[\'dirname\']."/".$newfilename.".".$path[\'extension\'];

  rename($file, $newfile);    

  $wp_filetype = wp_check_filetype(basename($newfile), null );
  $wp_upload_dir = wp_upload_dir();
  $attachment = array(
    \'guid\' => $wp_upload_dir[\'url\'] . \'/\' . basename( $newfile ), 
    \'post_mime_type\' => $wp_filetype[\'type\'],
    \'post_title\' => preg_replace(\'/\\.[^.]+$/\', \'\', basename($newfile)),
    \'post_content\' => \'\',
    \'post_status\' => \'inherit\'
   );
  $attach_id = wp_insert_attachment( $attachment, $newfile, $parent->ID );
  require_once(ABSPATH . \'wp-admin/includes/image.php\');
  $attach_data = wp_generate_attachment_metadata( $attach_id, $newfile );
  wp_update_attachment_metadata( $attach_id, $attach_data );
}
DON\'T USE THIS FUNCTION !!!

1 个回复
最合适的回答,由SO网友:schwarzgrau 整理而成

尝试将函数添加到publish_post 创建了很多不必要的文件kaisers function 满足我的需要。

function modify_uploaded_file_names( $image ) {
    // Use part of the post or user object to rename the image
    get_currentuserinfo();
    global $post, $current_user;

    // only do this if we got the post id, 
    // otherwise they\'re probably in the media section 
    // rather than uploading an image from a post
    if ( isset( $_REQUEST[\'post_id\'] ) ) {
        // get the ID
        $post_id  = absint( $_REQUEST[\'post_id\'] );

        // get the post OBJECT
        $post_obj  = get_post( $post_id );

        // get the post slug
        $post_slug = sanitize_title($post_obj->post_title); 

        // get the author
        $author = sanitize_title( get_the_author_meta( \'display_name\', $post_obj->post_author ) );

        switch( $image[\'type\'] ) {
            case \'image/jpeg\' :
                $suffix = \'jpg\';
                break;

            case \'image/png\' :
                $suffix = \'png\';
                break;

            case \'image/gif\' :
                $suffix = \'gif\';
                break;
        }

        // if we found a slug
        if ( $post_slug ) 
            $image[\'name\'] = "{$author}-{$post_slug}-{$random_number}.{$suffix}";
    }
    else {
        $image_name    = str_place( \' \', \'-\', strtolower( $current_user->data->user_nicename ) );
        $image[\'name\'] = $image_name . \'-\' . $file[\'name\'];
    }

    return $image;
}

// Only one arg, so 4th attr not needed - Priority set to later 20
add_filter( \'wp_handle_upload_prefilter\', \'my_modify_uploaded_file_names\', 20 );
只有一些小的变化

已替换$post_obj->post_name 具有sanitize_title($post_obj->post_title) 因为post\\u name只在我的测试中存在,而post已经保存wp_handle_upload 使用wp_unique_filename() 无论如何

结束