I'm using this form to replace featured images and post title on my frontend form:

<form id="featured_upload" name="featured_upload" method="post" action="#" enctype="multipart/form-data">
<p><label for="title">Title</label><input type="text" id="title" value="<?php echo $post_to_edit->post_title; ?>" size="45" name="title" /></p>
<input type="file" name="my_image_upload" id="my_image_upload" multiple="false" required />
<p><input type="hidden" name="post_id" id="post_id" value="" /></p>
<?php wp_nonce_field( 'my_image_upload', 'my_image_upload_nonce' ); ?>
<input id="" name="" type="submit" value="Upload"/>

Here's the saving process

// Check the nonce is valid, and the user can edit this post.
if ( isset( $_POST['my_image_upload_nonce'], $_POST['post_id'] ) 
    && wp_verify_nonce( $_POST['my_image_upload_nonce'], 'my_image_upload' )
    && current_user_can( 'manage_options', $_POST['post_id'] )) {
    include('wp-admin/includes/image.php' );
    include('wp-admin/includes/file.php' );
    include('wp-admin/includes/media.php' );
    // Let WordPress handle the upload.

    $attachmentID = get_post_thumbnail_id();                            
    $attachment_path = get_attached_file( $attachmentID);                   
    //Delete attachment from database only, not file
    $delete_attachment = wp_delete_attachment($attachmentID, true);
    //Delete attachment file from disk
    $delete_file = unlink($attachment_path);
    //Form attachment Field name.
    $file_handler = 'my_image_upload'; 
    $attach_id = media_handle_upload( $file_handler, $_POST['post_id'] );

    //changing the post title
    if(!empty($_POST['title'])) {
    //$new_title = sanitize_title($_POST['title']);
    $new_slug = sanitize_title($_POST['title']);        
    $my_post = array('post_title' => $_POST['title'],'post_name'  => $new_slug);
    // Update new post title into the database
    wp_update_post( $my_post );

The problem:

I need the uploaded picture to be saved with the new post title ($_POST['title'])


The chosen image is image1.jpg

The new post name is renamed post (just like on $_POST['title'])

On the upload process, image1.jpg should be renamed to renamed-post.jpg


You can use sanitize_file_name filter to rename file. put this code in active theme's functions.php, this will rename image filename as postname if only get title request. I have tested this code and it is working fine. Post name : https://prnt.sc/q3thzw Uploaded renamed image : https://prnt.sc/q3tib7

function make_filename_as_post_name($filename) {
    $info = pathinfo($filename);
    $ext  = empty($info['extension']) ? '' : '.' . $info['extension'];
    $name = basename($filename, $ext);
    $new_name = $_REQUEST['title'];
        # Replace space with dash 
        $new_name = preg_replace('/\s+/', '-', $new_name);
        return $new_name . $ext;
        return $name . $ext;
add_filter('sanitize_file_name', 'make_filename_as_post_name', 10);

let me know if this works for you!

| improve this answer | |
  • The $_REQUEST['title'] refers to the title field on my form, right? This would not change image name when they are uploaded on wp-admin back-end, right? – bpy Nov 29 '19 at 12:46
  • 1
    Yes. i have tested. – Chetan Vaghela Nov 29 '19 at 12:59
  • You have tested to replace only image names submitted on the form with input field with ID title? Sorry, but I have to be sure on this... – bpy Nov 29 '19 at 13:27
  • 1
    yes i have submitted form and image name renamed as post name. i have also upload image using media uploader, i have not found any issue in that. let ne know if this works for you. – Chetan Vaghela Nov 29 '19 at 13:46
  • 1
    Thanks!! It works! – bpy Nov 29 '19 at 13:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.