Skip to main content
Tweeted twitter.com/#!/StackWordPress/status/318405838417051649
deleted 7 characters in body; edited tags
Source Link
fuxia
  • 107.1k
  • 38
  • 255
  • 459

Here's the function I use for WP to rename images during upload on the fly and set the image's filename to match the post slug.

function wpsx_5505_modify_uploaded_file_names($arr) {

// Get the parent post ID, if there is one
if( isset($_REQUEST['post_id']) ) {
    $post_id = $_REQUEST['post_id'];
} else {
    $post_id = false;
}

// 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($post_id && is_numeric($post_id)) {

    // Get the post slug
    $post_obj = get_post($post_id); 
    $post_slug = $post_obj->post_name;

    // If we found a slug
    if($post_slug) {

        $random_number = rand(10000,99999);
        $arr['name'] = $post_slug . '-' . $random_number . '.jpg';

    }

}

return $arr;

}
add_filter('wp_handle_upload_prefilter', 'wpsx_5505_modify_uploaded_file_names', 1, 1);

I want to keep the original filename adding $ post_slug

[thread_title]-[original_filename].ext

thx

Here's the function I use for WP to rename images during upload on the fly and set the image's filename to match the post slug.

function wpsx_5505_modify_uploaded_file_names($arr) {

// Get the parent post ID, if there is one
if( isset($_REQUEST['post_id']) ) {
    $post_id = $_REQUEST['post_id'];
} else {
    $post_id = false;
}

// 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($post_id && is_numeric($post_id)) {

    // Get the post slug
    $post_obj = get_post($post_id); 
    $post_slug = $post_obj->post_name;

    // If we found a slug
    if($post_slug) {

        $random_number = rand(10000,99999);
        $arr['name'] = $post_slug . '-' . $random_number . '.jpg';

    }

}

return $arr;

}
add_filter('wp_handle_upload_prefilter', 'wpsx_5505_modify_uploaded_file_names', 1, 1);

I want to keep the original filename adding $ post_slug

[thread_title]-[original_filename].ext

thx

Here's the function I use for WP to rename images during upload on the fly and set the image's filename to match the post slug.

function wpsx_5505_modify_uploaded_file_names($arr) {

// Get the parent post ID, if there is one
if( isset($_REQUEST['post_id']) ) {
    $post_id = $_REQUEST['post_id'];
} else {
    $post_id = false;
}

// 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($post_id && is_numeric($post_id)) {

    // Get the post slug
    $post_obj = get_post($post_id); 
    $post_slug = $post_obj->post_name;

    // If we found a slug
    if($post_slug) {

        $random_number = rand(10000,99999);
        $arr['name'] = $post_slug . '-' . $random_number . '.jpg';

    }

}

return $arr;

}
add_filter('wp_handle_upload_prefilter', 'wpsx_5505_modify_uploaded_file_names', 1, 1);

I want to keep the original filename adding $ post_slug

[thread_title]-[original_filename].ext

Source Link
Daniela
  • 59
  • 1
  • 2

Rename attachments during upload

Here's the function I use for WP to rename images during upload on the fly and set the image's filename to match the post slug.

function wpsx_5505_modify_uploaded_file_names($arr) {

// Get the parent post ID, if there is one
if( isset($_REQUEST['post_id']) ) {
    $post_id = $_REQUEST['post_id'];
} else {
    $post_id = false;
}

// 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($post_id && is_numeric($post_id)) {

    // Get the post slug
    $post_obj = get_post($post_id); 
    $post_slug = $post_obj->post_name;

    // If we found a slug
    if($post_slug) {

        $random_number = rand(10000,99999);
        $arr['name'] = $post_slug . '-' . $random_number . '.jpg';

    }

}

return $arr;

}
add_filter('wp_handle_upload_prefilter', 'wpsx_5505_modify_uploaded_file_names', 1, 1);

I want to keep the original filename adding $ post_slug

[thread_title]-[original_filename].ext

thx