0

I have a problem I need to fix with the URLs that are generated for the attachment pages of images with metadata.

I’m working on a photography site where individual photos are displayed as attachments to a post, via attachment.php.

The URL scheme for displaying these photos is something like this:

domain.com/project-name/attachment/attachment-name.

If I upload an image without any metadata, then the last part of the attachment URL is taken from the file name of the image. So, if the image is ABCD.jpg, then the URL is domain.com/project-name/attachment/ABCD. All good.

However, if the image does have a title in the metadata, then the URL is set to that title: domain.com/project-name/attachment/omg-cool-photo.

Is it possible to have a metadata title with a photo, but to also have the URL that is set for the attachment to remain as the photo filename? If so, how?

I took a look around but I couldn’t see anything about this here already. Sorry if it is and I missed it.

Any help gratefully received!

Thanks.

1 Answer 1

0

You can hook onto add_attachment and force the slug to work off the filename like so:

Update: To uppercase the slug, you'll need to add a temporary filter onto wp_unique_post_slug. You can't simply use strtoupper( $slug ) as WordPress sanitises (lowercases) it within the update function.

/**
 * Force attachment slug to ignore title from metadata (if any).
 * 
 * @link http://wordpress.stackexchange.com/q/140132/1685
 * 
 * @param int $post_id
 */
function wpse_140132_attachment_slug_as_file_name( $post_id ) {
    if ( $post = get_post( $post_id ) ) {
        if ( $file = get_attached_file( $post->ID ) ) {
            // Get file name, without extension.
            $slug = pathinfo( $file, PATHINFO_FILENAME );

            // Only update if different to current slug!
            if ( $slug && $slug != $post->post_name ) {
                add_filter( 'wp_unique_post_slug', 'wpse_140132_attachment_slug_as_file_name_uppercase', 50 );
                wp_update_post(
                    array(
                        'ID' => $post->ID,
                        'post_name' => $slug,
                    )
                );
                remove_filter( 'wp_unique_post_slug', 'wpse_140132_attachment_slug_as_file_name_uppercase', 50 );
            }
        }
    }
}

add_action( 'add_attachment', 'wpse_140132_attachment_slug_as_file_name' );

/**
 * @see     wpse_140132_attachment_slug_as_file_name()
 * 
 * @param   string  $slug
 * @return  string
 */
function wpse_140132_attachment_slug_as_file_name_uppercase( $slug ) {
    return strtoupper( $slug );
}
2
  • Thank you, kind internet sir! Is there any way to also easily get the letters in the filename to be in uppercase in the URL? Yes, I know it sounds crazy... Commented Apr 3, 2014 at 15:18
  • That's a little trickier, but not impossible. See revised answer. Commented Apr 3, 2014 at 16:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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