• When you visit an attachment page, you find yourself at the address: “site.com/category/articlename/attachment/attachmentname/

    Is there a way to change how WordPress names ‘attachment’ so that it could say something else in the address?

Viewing 6 replies - 1 through 6 (of 6 total)
  • You can probably change it by using a filter:

    function wpd_attachment_link( $link, $post_id ){
        $post = get_post( $post_id );
        return home_url( '/images/' . $post->post_title );
    }
    add_filter( 'attachment_link', 'wpd_attachment_link', 20, 2 );

    You can check this thread on Stack Overflow for more info:
    https://wordpress.stackexchange.com/questions/14924/pretty-urls-or-permalinks-for-attachments#answer-14931

    Thread Starter ordresser

    (@ordresser)

    This code does in fact create a link, but the link doesn’t really work, unfortunately.

    After adding the function, I uploaded a new image, then added the image (as an attachment) to an article. When I click the image (which used to go to the site.com/category/articletitle/attachment/attachmentname/ image, it now goes to site.com/images/imagename, but there’s nothing on the page (displays a 404).

    The good thing about this code, though, is it doesn’t remove the ‘attachment’ address from other images already on the site, which is good because that means it won’t ruin existing links that can still go to the /attachment/ URLs, because sometimes WordPress updates and it would be a mess if the change from /attachment/ caused a problem with an update. After placing the code, I can still go to the category/title/attachment/imagename/ on the new image and it works. (Only the new domain with /images/imagename doesn’t work/goes to 404.

    • This reply was modified 6 years, 2 months ago by ordresser.

    Do you see anything weird in your logs? did you try flushing your permalinks and clearing your cache after adding your code?

    Thread Starter ordresser

    (@ordresser)

    I did clear WordPress’s cache. I’ve never heard of flushing permalinks, and I don’t know exactly how you’d do that

    Thread Starter ordresser

    (@ordresser)

    OK, I flushed the permalinks (and cleared the cache). The result was: Server Error for the whole site, which after I removed the code from my functions.php the site came back.

    Then, after putting your code back in the functions.php and re-flushing permalinks again and clearing the cache again, the website didn’t break that time, but still, the site.com/images/imagename just shows a 404.

    • This reply was modified 6 years, 2 months ago by ordresser.
    Thread Starter ordresser

    (@ordresser)

    I’ve been trying to get your code to work, but just keep coming up against a (it think small) problem:

    without code (regular WordPress):

    viewing an attachment page for an image goes to:

    site.com/category/article-name/ATTACHMENT/image-name

    … and there’s the image on the attachment page.

    —————————

    I’ve added a second piece of code by Milo:

    function wpd_rename_attachment_rewrites( $rules ){
        foreach( $rules as $key => $value ){
            $new_key = str_replace( 'attachment', 'new-word', $key );
            unset( $rules[ $key ] );
            $rules[ $new_key ] = $value;
        }
        return $rules;
    }
    add_filter( 'rewrite_rules_array', 'wpd_rename_attachment_rewrites' );

    Together, the code does 2 things: 1) change the way attachments are permalinked and 2) change the way they are reached. After adding the function and refreshing permalinks/cache:

    Wiewing attachment page for an image goes to:

    site.com/IMAGE/image-name

    … which is nothing there because its a different URL from the correct new attachment permalink, so its an Error404 on the front end when you visit that URL.

    However, you can get to the correct URL by typing in:

    site.com/category/article-name/IMAGE/image-name

    … and brings up the correct attachment page.

    So is there a way your code can be adjusted a bit so that when you click on the attachment for an image, it goes not to site.com/IMAGE/image-name but instead goes to site.com/category/article-name/IMAGE/image-name (ie it adds category/article-name to the URL direction)?

    • This reply was modified 6 years, 2 months ago by ordresser.
Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Does ‘attachment’ have to be called ‘attachment’?’ is closed to new replies.