Имена загружаемых файлов как заголовок поста


В functions.php используемой темы добавляем:

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';
			$arr['name'] = $post_slug . '-' . $random_number .'.'. pathinfo( $arr['name'], PATHINFO_EXTENSION );
        }
    }
    return $arr;
}
add_filter('wp_handle_upload_prefilter', 'wpsx_5505_modify_uploaded_file_names', 1, 1);
//add_filter('save_post', 'wpsx_5505_modify_uploaded_file_names', 1, 1);

function wpsx_5505_modify_uploaded_file_meta($meta, $file, $sourceImageType) {

    // 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 title
        $post_title = get_the_title($post_id);

        // If we found a title
        if($post_title) {
            $meta['title'] = $post_title;
            $meta['caption'] = $post_title;

        }
    }
    return $meta;
}
add_filter('wp_read_image_metadata', 'wpsx_5505_modify_uploaded_file_meta', 1, 3);

В итоге нужно создать пост и сохранить его (возможно как черновик) и только после этого добавлять к посту всякие медиа-файлы. При этом имя и описания (alt + title) файлов будут равны названию поста.