0

I'm using WordPress as a way to keep track of several different types of data which all have different extension names -- file1.fig, file2.dat, etc. When I upload a file and insert it into a blog post, I'd like to see the full filename of the file + the filename extension. Currently, when I upload "myfile.dat", Wordpress automatically generates a title for the file based on the filename--the Attachment Details will show that its Title is "myfile". When I then insert the media into my blog post, it creates a nice link to the file but the link has the text matching the Title ("myfile"), which does not include the filename extension.

Essentially, when I upload "myfile.dat" and insert it into my blog post, I would like the link text to be the full filename WITH the extension--link text of "myfile.dat" not just "myfile". How can I either have wordpress format the Title correctly upon upload, or change the way it inserts the media into the post (e.g. have it paste the attachment filename instead of the Title as the link text)

0

I figured out how to retitle the attachment myself, using a combination of the answers from these two posts:

How can I add a default description to uploaded files?

Change attachment filename

Here's the code, it's working on my current WP installation (v4.3)

function wpse_retitle_attachment( $post_ID )
{
    $file = get_attached_file($post_ID);
    $path = pathinfo($file);
    $args = array( 
                    'ID'           => $post_ID, 
                    'post_title'   => $path['filename'] . "." . $path['extension'], 
                    // 'post_excerpt' => 'My default caption ...', 
                    // 'post_content' => 'My default description ...', 
            );

    wp_update_post( $args );

}
add_action( 'add_attachment', 'wpse_retitle_attachment' );

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.