post meta - Rename image filename using 'media_handle_upload'

admin2025-01-07  6

I need to rename images uploaded by the user when they post on my site.

I have a code that works perfectly to save the image and make it part of the post. But I want the images to be renamed to have the title of the post in their filename.

The code I have:

require_once( ABSPATH . 'wp-admin/includes/image.php' );
require_once( ABSPATH . 'wp-admin/includes/file.php' );
require_once( ABSPATH . 'wp-admin/includes/media.php' );
$img_id01 = media_handle_upload( 'img_main', $postID );
update_post_meta($postID, 'img_id01', $img_id01);

I need to rename images uploaded by the user when they post on my site.

I have a code that works perfectly to save the image and make it part of the post. But I want the images to be renamed to have the title of the post in their filename.

The code I have:

require_once( ABSPATH . 'wp-admin/includes/image.php' );
require_once( ABSPATH . 'wp-admin/includes/file.php' );
require_once( ABSPATH . 'wp-admin/includes/media.php' );
$img_id01 = media_handle_upload( 'img_main', $postID );
update_post_meta($postID, 'img_id01', $img_id01);
Share Improve this question asked Sep 26, 2018 at 16:30 Marcelo Henriques CortezMarcelo Henriques Cortez 6241 gold badge7 silver badges27 bronze badges 8
  • 'update_post_meta' adds the image to the post 'img_id01' field. That post is being created by the user in the front-end. The images are inserted via upload field and I use the 'media_handle_upload' to handle the file upload POST request, and to create the attachment post in the database. But if I don't use the 'update_post_meta' the image don't get inserted in the post. – Marcelo Henriques Cortez Commented Sep 26, 2018 at 16:40
  • I want to rename the image before its saved on the server. Don't want to 'edit' it after the post is created and the images are attached. – Marcelo Henriques Cortez Commented Sep 26, 2018 at 16:42
  • Looking at the function that handles the upload, I can see a call to the function wp_unique_filename(), which in itself has a filter wp_unique_filename applied before returning the value. – Hans Commented Sep 26, 2018 at 16:55
  • Ok, I see it. But I have no idea how to use that with the code I have. Any hints? – Marcelo Henriques Cortez Commented Sep 26, 2018 at 17:01
  • You need to make sure that images only get renamed in that specific use case, and not while you upload stuff ie. in the Media Library. But I don't know your code, so it's hard for me to tell. – Hans Commented Sep 26, 2018 at 17:04
 |  Show 3 more comments

1 Answer 1

Reset to default 0

I cannot test this because I don't have your code, but maybe you can get some ideas from it:

add_filter( 'wp_unique_filename', 'custom_image_name', 10, 2 );
$img_id01 = media_handle_upload( 'img_main', $postID );
remove_filter( 'wp_unique_filename', 'custom_image_name', 10, 2 );

function custom_image_name( $filename, $ext ) {

    global $postID;
    $post = get_post( $postID );
    return $filename . '-' . $post->post_name . $ext;
}

This is not very elegant because $postID needs to be in a global scope, but it might do the job.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736256811a391.html

最新回复(0)